Ruby on Rails Thursday, May 28, 2015



On Thursday, May 28, 2015 at 12:18:42 PM UTC+1, Psycho Shine wrote:
SQLite3::SQLException: no such column: disputes.user_id: SELECT COUNT(*) FROM "disputes"  WHERE "disputes"."user_id" = ? AND "disputes"."indicted_id" = 1

How I think, when I build a call like @user.disputes ActiveRecord try to find  a column with user_id in dispute model, but I have :claimant_id and :indicted_id instead :user_id
It's my first time with using foreign_key, so I can make mistake in design. Please help me fix it. Thank you

Yes, active record assumes that the foreign key to use is user_id.If the association should use a different key then you must tell active record, e.g.

    has_many :users, foreign_key: 'claimant_id'

You could also do

    has_many :claimants, class_name: 'User' #don't need to set foreign key because default is inferred from the association

Fred 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2ea099fd-8ff1-4b91-bef4-e1c139e73677%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment