activerecord - belongs_to with a custom class_name not producing proper foreign key in Rails 3 -
I'm updating Rail 3 to an app and I'm having trouble creating a custom foreign key. Is:
class product & lt; ActiveRecord :: Base is_to: Owner, class_name = & gt; 'User' ... and Class User & lt; ActiveRecord :: Base has_many: Products ... End Class ProductsController & lt; ApplicationController before_filter: authenticate_user! Def Index = Products = Current_Uz. Products and End
View:
& lt;% - @ products.each do | P | -% & gt; & Lt;% = p.created_at% & gt; & Lt; Br / & gt; & Lt;% - end -% & gt;
I get this error in my rail log:
Mysql :: error: selection of 'clause' in unknown column 'products.user_id' Do `products`. Whereas (`products`. User_id = 1)
should look at the related_to: owner
and a foreign key is called Owner_id
. I also tried to establish a foreign key clearly and it is not working. I probably tested the light for a train 3 bug but no luck.
You must specify the foreign key has_many: products
on the association, this Automatically do not know that this related_to: owner
mirror does.
This should work:
class users & lt; ActiveRecord :: Base has_many: products,: foreign_key => 'owner_id' ... end
From the rail docs:
: foreign_key < / Strong>
Specify the foreign key used for the association. By default, this class name is approximate in lower-case and "_id" is suffixed. Therefore, a person class who has_many Association will use "person_id" as the default: foreign_key ..
Comments
Post a Comment