I have BudgetLine model. The model has budget_item_id column.
Second model is BudgetItem. it has an id and a column: full_name.
Now, I want to call: budget_line.item_name and get the name that is corresponding to the full_name in BudgetItem.
I tried (in BudgetLine):
has_one :item_name, :class_name => 'BudgetItem', :foreign_key => 'budget_item_id'
But it is not working.
Since your foreign key is in BudgetLine model, you should have belongs_to association in it:
belongs_to :item_name, class_name: 'BudgetItem', foreign_key: 'budget_item_id'
if you want to have method that returns only item_name, I'd advice renaming your association and adding separate method:
belongs_to :budget_item
def item_name
budget_item.try(:full_name)
end
instead of writing in BudgetLine
has_one :item_name, :class_name => 'BudgetItem', :foreign_key => 'budget_item_id'
You should write
belongs_to :item_name, :class_name => 'BudgetItem', :foreign_key => 'budget_item_id'
Related
I have these model definitions:
class Property < ActiveRecord::Base
belongs_to :user
has_many :users_favourites_properties, class_name: 'UsersFavouritesProperties', dependent: :destroy, :foreign_key => :property_id
has_many :favourites, :class_name => "User", through: :users_favourites_properties
end
class User < ActiveRecord::Base
has_many :properties
has_many :users_favourites_properties, class_name: 'UsersFavouritesProperties', dependent: :destroy, :foreign_key => :user_id
has_many :favourites, class_name: "Property", through: :users_favourites_properties
end
class UsersFavouritesProperties < ActiveRecord::Base
self.table_name = "favourites"
belongs_to :favourites, class_name: 'User', :foreign_key => :user_id, :primary_key => :id
belongs_to :favourites, class_name: 'Property', :foreign_key => :property_id, :primary_key => :id, counter_cache: :users_count
end
When I execute:
current_user.favourites << property
The UsersFavouritesProperties object created has the same user_id and property_id because the user_id is beign setted with the property_id.
Do you know what is happening?
You seem to misunderstand what the association name is for.
Each association method call (like has_many) adds certain methods to a model that allow for easy lookup for associated objects.
The name of association means (in scope of a model) "what is it for me". A symbol you specify first is an association name, and it is used (among other things) as a name of a method that you will use to access that association.
By defining an association named favourites once, you've defined the methods for UsersFavouritesProperties. The second time you define an association with the same name, some of the first one is redefined and crazy things can happen.
So, following that "what is it for me" principle, you can figure that a User associated with a certain UsersFavouritesProperty is UsersFavouritesProperty's user, and the resulting association, that you have:
belongs_to :favourites, class_name: 'User', :foreign_key => :user_id, :primary_key => :id
should be more like simple:
belongs_to :user
The rest is not even needed, as it follows the Rails' convention: class name and foreign key can be derived from association name in standard ways (capitalize for class name, add _id for foreign key), primary key is default.
I strongly recommend you read the Rails Style Guide and let ActiveRecord guess the most you can provide.
These lines
belongs_to :favourites, ...
belongs_to :favourites, ...
Should be
belongs_to :user, ...
belongs_to :property, ...
You can't use two keys for belongs_to
i.e. Post model has a column poster_id and for posters I'm actually using User model. Obviously this is a User has_many Posts relationship, but how to I specify that poster_id is actually an id to User model?
Use foreign_key option:
has_many :posts, :foreign_key => :poster_id
For Post model it will be
belongs_to :user, :foreign_key => :poster_id
or
belongs_to :poster, :class_name => 'User'
I have two models User and Dtype.
In users table I have two column named dtype_id and dtype_second which contains ID's of Dtype. Now I need to get both the columns values through association.
Using #user.dtype.name will give me the value of first column (dtype_id).
Now, I also need the value of second column i.e dtype_second.
For this I'm trying this:
In User Model:
belongs_to :dtype
In Dtype Model:
has_many :users
has_many :dtype_seconds, :foreign_key => 'dtype_second', :class_name => "User"
On view:
<%= #user.dtype_seconds.name %>
The issue is that I'm not getting any value or error.
First. Naming. You should rename dtype_second field to dtype_second_id
Next. Associations. You need appropriately describe both associations in both models
class User
belongs_to :dtype
belongs_to :dtype_second, :class_name => 'Dtype'
end
class Dtype
has_many :users
has_many :second_users, :class_name => 'User', :foreign_key => 'dtype_second_id'
end
Your are adding a has many to dtype, but you're trying to call the method on a user. So you have to define the dtype_second association on the user:
class User < ActiveRecord::Base
belongs_to :dtype
belongs_to :dtype_alternative, :foreign_key => 'dtype_second', :class_name => "Dtype"
...
end
<%= #user.dtype_alternative.name %>
I have the follow two models:
User
Mentions (user_id, mentioned_by)
#mention.user gives me the creator, but I need the user who was mention. I would like to do: #mention.mentioner which gets the user_id with mentioned_by
How do I set that relationship in the models? I tried:
User.rb:
has_many :mentions
Mention.rb
belongs_to :user
belongs_to :user, :as => "mentioner", :foreign_key => "mentioned_by"
But that errors. Ideas? Thanks
In mention.rb, try this:
belongs_to :user
belongs_to :mentioner, :class_name => "User", :foreign_key => "mentioned_by"
I have multiple models with created_by and modified_by columns. This is what I have for a Deal Model.
class Deal
has_one :user , :foreign_key => 'created_by'
has_one :user , :foreign_key => 'modified_by'
end
class User
belongs_to :created_by , :class_name => 'Deal' , :foreign_key => 'created_by'
belongs_to :modified_by , :class_name => 'Deal' , :foreign_key => 'modified_by'
end
When I create the deal, looks like it is saving correctly. But in the show view when I try to get #deal.created_by.email I get an "undefined method email" error. Can some tell me how to get this working please?
Also since I have multiple models with these two columns, there can be many belongs_to in User model. Is there an elegant solution for this case?
First thing you have to add is the specification of accessible attributes.
In User you would have to add:
attr_accessible :email, :created_by, :modified_by
In Deal:
attr_accessible :created_by, :modified_by
But you should also change the direction of your relation. The foreign_key is always on the belongs_to side.
This is what worked for me:
class Deal < ActiveRecord::Base
belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
belongs_to :modified_by, :class_name => "User", :foreign_key =>"modified_by"
attr_accessible :created_by, :modified_by, :name
end
class User < ActiveRecord::Base
has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
attr_accessible :created_deals, :modified_deals, :name
end
If you have more models, which look similiar you could probably use polymorphic associations: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
First of all, from my experience it is generally a bad idea to have associations using the foreign key as name. Especially when writing fixtures it seems rails will get confused between setting the actual value "created_by" or the model in the created_by association. In my models I generally use these associations for the cases you describe:
belongs_to :creator, :class_name => "User", :foreign_key => 'created_by'
belongs_to :modifier, :class_name => "User", :foreign_key => 'modified_by'
You can use association names like 'creating_user' instead if you prefer. If you really want created_by as association name you should have created_by_id or something similar as foreign key, just as long as its not equal to the association name.
Then I am a bit confused by your pasted code. Your choice "Deal has_one User" and "User belongs_to Deal" means that the users table will have the columns created_by and modified_by (foreign keys) containing Deal Ids, basically meaning that users get created by a single deal? However it seems like deals should get created by users and not the other way round. Your example of deal.created_by.email can not work at all with your associations, since deal would not have an association called "created_by", only "user", of which you have two associations with the same name in a single model which can not work at all in the first place.
Fixing your associations similar to what Patrick suggested:
class Deal < ActiveRecord::Base
belongs_to :creator, :class_name => "User", :foreign_key => "created_by"
belongs_to :modifier, :class_name => "User", :foreign_key =>"modified_by"
end
class User < ActiveRecord::Base
has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
end