All Questions

Tagged with
Filter by
Sorted by
Tagged with
41 votes
7 answers
48k views

Rails Model without a table

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google. My question is: How can I put the colors in ...
user993460's user avatar
39 votes
6 answers
9k views

Rails Models: how would you create a pre-defined set of attributes?

I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For ...
Andrew's user avatar
  • 42.8k
37 votes
4 answers
17k views

What is the Rails3 version of errors.add_to_base?

I want to write a custom validation in Rails 3, but following this example I get: 'ActiveModel::DeprecatedErrorMethods.add_to_base' call is deprecated in Rails 3.0 What is the rails3 version of: ...
Marius Butuc's user avatar
  • 18.1k
27 votes
1 answer
7k views

How to use different authentication_keys for two devise models?

Now I have one devise model, which uses email as authentication_key. I want to add one new devise model, which uses student_id as authentication_key. Some guide tells me to modify the configuration ...
Roy's user avatar
  • 271
24 votes
4 answers
20k views

Rails - How to validate a field only if a another field has a certain value?

In my form I have a Select with three values: Apple, Banana and Cherry. If I choose Apple from the select I hide another Select- and a Text-field with some Javascript, because when Apple is chosen, ...
TehQuila's user avatar
  • 658
23 votes
2 answers
11k views

How to use routing helpers in a model with Rails 3?

In a model callback I make an API call and need to give the API some URLs. Those URLs would be easily generated by a routing helper. The solution given in this answer does not work anymore in Rails 3....
Florent2's user avatar
  • 3,483
20 votes
4 answers
24k views

Cannot skip validation in Rails 3?

I'm working on a project in Rails 3 where I need to create an empty record, save it to the database without validation (because it's empty), and then allow the users to edit this record in order to ...
Andrew's user avatar
  • 42.8k
17 votes
2 answers
19k views

Roll back all rails migrations or drop tables and modify migrations (start from scratch)

I'm new to Rails and have started a project that I'm unhappy with my models and db schema. I'd like to start again from scratch but keep all my views, controllers. What's the best way to go about ...
AdamT's user avatar
  • 6,475
17 votes
7 answers
17k views

How to create ActiveRecord tableless Model in Rails 3

I am trying to create a Active Record tableless Model. My user.rb looks like this class User < ActiveRecord::Base class_inheritable_accessor :columns def self.columns @columns ||= []; ...
Achaius's user avatar
  • 6,054
17 votes
1 answer
21k views

Rails: How do self-referential has_many models work?

So, I'm working on an app where I want to users to be able to group objects in "folders". Basically: User has_many :foos Foos don't have to be in a folder, but they can be. In that case: Folder ...
Andrew's user avatar
  • 42.8k
17 votes
6 answers
11k views

Rails 3 validate uniqueness of one value against a column?

I have a model with an active column, which is a boolean. I want to validate the uniqueness of all newly added records against company_id, such that I can add as many records as I want with the same ...
neezer's user avatar
  • 20.2k
15 votes
2 answers
13k views

How to validate uniqueness in Rails 3 Model if I want to check if there is a 2-field combination?

I want to know if there is a way in Rails 3, in which I can validate the uniqueness of 2 fields that goes in combination. The logic goes this way: I have two fields employee_code and date_entry. ...
johan's user avatar
  • 2,329
15 votes
6 answers
11k views

What is purpose of around_create callback in Rails Model?

when is around_create callback code executed, in what situations we should use it?
Maddy.Shik's user avatar
  • 6,707
14 votes
2 answers
3k views

Rails 3 - validating one of two fields has been completed

I'm writing a Rails app to manage a series of banners along the top of our website; each one should link either to a URL provided by the editor, or a specific product selected from a drop-down list. ...
PaulC's user avatar
  • 491
12 votes
2 answers
10k views

rails g scaffold for existing model and DB table

I would like to create a structure with rails g scaffold Article, but I have already created the table Articles and model Articles. Is there any way to do that?
user984621's user avatar
  • 47.4k
12 votes
1 answer
8k views

How to delete model and scaffolding in RoR?

How can I delete an existing model and scaffolding in ruby on rails 3? Can I just nuke the folders?
femi's user avatar
  • 984
12 votes
1 answer
9k views

Rails 3 - how to save (un)checked checkboxes?

I have in a form (form_tag) several checkboxes like this: <%=check_box_tag 'model_name[column_name]', 1, (@data.model_name.column_name == 1 ? true : false)%> And updating them like: variable =...
user984621's user avatar
  • 47.4k
12 votes
2 answers
1k views

has_one, :through => model VS simple method?

I have some issues using has_one, through => model. The best is to show you my case. class Category has_many :articles end class Article has_many :comments belongs_to :category end class ...
Hartator's user avatar
  • 5,127
11 votes
5 answers
14k views

get model attribute dynamically in rails 3

How can I get the attribute from the model object dynamically? I have the attribute of the User object as string: u = User.find(1) Can I do something like u.get("user_id")
ed1t's user avatar
  • 8,599
10 votes
1 answer
6k views

rails how to properly move models to a namespace

I'm running rails v 3.2 . After some time developing application I need to move some models into namespace ex: /app/models/address.rb to /app/models/local/address.rb I tried simply moving models to ...
Elmor's user avatar
  • 4,835
10 votes
3 answers
8k views

Duplicating a record in Rails 3

I have a prescription model in my Rails 3 application. I am trying to work out the best method of allowing records to be duplicated, but allowing the user to "review" the duplicate before it's saved. ...
dannymcc's user avatar
  • 3,784
10 votes
2 answers
4k views

Merging ActiveAdmin users with existing user model

I've set up ActiveAdmin early in my project and used the default admin_users model for authentication. I've since used Devise to set up a separate User model and have realized it would probably be ...
Kyle Macey's user avatar
  • 8,144
9 votes
2 answers
4k views

Load fixtures for specific model

For automated testing, RSpec and FactoryGirl are used. Frequently, I need to manually play with my application. So, i need a convenient way to populate database with some data. The most convenient ...
AntonAL's user avatar
  • 17k
9 votes
1 answer
8k views

find_by_sql with array format in Rails 3

good day guys! I'm using find_by_sql() in rails 3 to fetch records as follows. @list=Email.find_by_sql(["SELECT * FROM Emails WHERE sent_id=?",params[:id]]) How to modify the same statement if ...
Unknown Coder's user avatar
9 votes
2 answers
28k views

How do you pass data from a controller to a model with Ruby on Rails?

How do you pass data from a controller to a model? In my application_controller I grab the user's location (state and city) and include a before_filter to make it accesible in all my controllers via ...
Laser's user avatar
  • 5,259
8 votes
5 answers
4k views

conditional to_param method

I am using the model instance method t_param to generate a SEO-style URL def to_param url end that way I can generate links to the model with path_to_model(model) and query the model with Model....
Mark's user avatar
  • 3,389
7 votes
4 answers
15k views

Rails Model: Name -- First, Last

I'm fairly new to rails, working on a Rails 3 app with a Profile model for users. In the profile Model I'd like to have a "name" entry, and I'd like to be able to access logical variations of it ...
Andrew's user avatar
  • 42.8k
7 votes
3 answers
15k views

Rails - Is there a way to update a single attribute?

I was just wondering, I have a model that, besides the id's from other models (FK), it has a single attribute boolean. I want to know how can I create a button that changes this boolean and just that ...
Samth's user avatar
  • 113
7 votes
1 answer
16k views

Using raw sql queries in Rails 3 application?

I am working on migrating a legacy database into my Rails application (3.2.3). The original database comes with quite a few long sql queries for reports. For now, what I would like to do it use the ...
dannymcc's user avatar
  • 3,784
7 votes
2 answers
622 views

Rails: Uniqueness of two attributes in join table causing 500 error

I have the following models, which basically are trying to mean that a professor has knowledge of many subjects for a particular level. The subjects are fixed, so there will be no new subjects ...
Nobita's user avatar
  • 23.6k
6 votes
3 answers
14k views

How to call helper method from model?

I use MongoDB as a database in my Rails application with MongoID gem. I want to call the helper method from the model within after_create callback method. How is it possible?My model code is: class ...
user avatar
6 votes
3 answers
12k views

calling a method in model from view

I am trying to evaluate which indicator needs to be displayed next to an item based on if it's been viewed before or not, new comments etc. Until I decide on a symbol to use, I just want a number to ...
bennett_an's user avatar
  • 1,718
6 votes
3 answers
3k views

Custom model method that should be included in JSON serialization

I'm working with a javascript library where I build comboboxes. I have the requirement of build a combobox with full name of a person, so I mean name + surname. Because in database those are 2 ...
Francesco Belladonna's user avatar
6 votes
4 answers
4k views

rails: create scaffold for models to inherit from superclass?

I'm new to Rails, still getting my feet wet, so please pardon me if this is either trivial or "the wrong way" to do things. I'd like to create a superclass for some scaffolded models. For example, I'...
allanberry's user avatar
  • 7,505
6 votes
5 answers
2k views

Using a JSON service as a Rails model

How can I use a JSON service as a model for a Rails 3.2 application? I would like to link model methods to JSON requests, e.g. MyModel.create to generate a JSON request to create a new record ...
gjb's user avatar
  • 6,257
6 votes
1 answer
1k views

Use one action for multiple models

I have about 5 models that behave very similarly. In fact, I'd like them to share an action for displaying them. For example, for models Car, Truck, Van I want to have a definition like: [Car, Truck, ...
Kyle Macey's user avatar
  • 8,144
6 votes
2 answers
3k views

Generate models from existing tables using Rails 3

Using Rails 3.2.2 and ruby 1.9.3dev and mysql I am new to ruby and rails. We have an existing database with a couple hundred tables. We would like to try out rails to see if it would be a positive ...
Michael's user avatar
  • 3,608
5 votes
1 answer
3k views

After_save with parameter

This is my model code: after_create :notify_cards_create after_destroy :notify_cards_destroy after_update :notify_cards_update def notify_cards_update WebsocketRails[:home].trigger 'cards', {type:...
John Smith's user avatar
  • 6,205
5 votes
2 answers
5k views

Self-referencing models in Rails 3

I have an Entity model and I want to display connections between the Entities. ie, Entity 1 is connected to Entity 2. My thinking, right now, is to create a join model between the two called ...
Slick23's user avatar
  • 5,867
5 votes
1 answer
4k views

Rails self-referential has_many through with custom naming of join table

I have some troubles wrapping my head around the following situation.. I am trying to create a tree structure, where I will be able to give custom names to connections between nodes.. So I want to ...
Stpn's user avatar
  • 6,344
5 votes
2 answers
337 views

Deep model nesting in Ruby on Rails

In quite a big project I encountered a problem of deep model nesting. For example there exists a model like this: class Communication::Conversation::ForProposal < Communication::Conversation::Base ...
makaroni4's user avatar
  • 2,281
4 votes
2 answers
1k views

Bound activerecords-queries using aggregate funcs (sum, avg,...) with limit

Given this pseudo-query: Model.sum(:column, :condition => ['id = ?', id], order => "date DESC", :limit => X) Here we have the problem that the sum obtained is not "limited" by the value X (...
damoiser's user avatar
  • 6,158
4 votes
3 answers
3k views

nested has_many :through in rails 3

I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2. I did come across a plugin that's pretty ...
Slick23's user avatar
  • 5,867
4 votes
2 answers
4k views

format a date in the rails model

I have a date of birth in the rails model and I display it in different places. Every time I have to specifically format it in mm/dd/yyyy format. Is there something I can do in my model so that ...
Omnipresent's user avatar
  • 29.8k
4 votes
4 answers
5k views

Rails association with tables without models

I'm looking to rewrite my project in Rails. It's currently written in PHP (CodeIgniter), I've come to the point where I'm writing more libraries and core extensions than I'm writing new code. I've ...
Saff's user avatar
  • 563
4 votes
2 answers
8k views

AssociationTypeMismatch (Object expected, got HashWithIndifferentAccess) in Rails app

I'm getting a AssociationTypeMismatch Error and I'm not sure where I'm making a mistake. I'm pretty new to Rails so I'm guessing I'm making some silly mistake. I've checked my syntax and compared it ...
John Chan's user avatar
  • 305
4 votes
2 answers
2k views

validation in rails with two date attributes

so i have a model with two date attributes # migration t.date :valid_from t.date :valid_until these are optional and you should be able to define only valid_from or valid_until, but if both ...
choise's user avatar
  • 25k
4 votes
2 answers
3k views

Rails not creating intermediary table? - Habtm relationship

The table between the User model and Period model does not exist. They have a has_many_and_belongs_to relationship. I'm learning Rails still, so please bear with me. This is the contents of the User....
Jarred Sumner's user avatar
4 votes
1 answer
195 views

Relationships in different namespaces

I'm a little stuck on how relationships are defined between models in different namespaces. I have created this example to illustrate my problem and have added comments with my requirements. class ...
adambutler's user avatar
4 votes
1 answer
732 views

Adding some custom methods to delayed job in rails 3?

I want to add some custom methods to the delayed jobs model. I want to have some extra such as... def status return "errored" unless self.last_error.blank? return "waiting" if self.locked_at....
CafeHey's user avatar
  • 5,760

1
2 3 4 5
13