I was wondering if there is a way to specify two foreign keys on a
has_many association. I have a conversation model with a sender_id and
recipient_id. A user can be the sender or recipient of a conversation.
To fetch all conversations I must specify the sender_id and
recipient_id on the User model, but I can only specify one of the two
as shown below.
class User < ActiveRecord::Base
has_many :conversations, :foreign_key => 'sender_id'
end
I was wondering if there is a way to specify two foreign keys on a
has_many association. I have a conversation model with a sender_id and
recipient_id. A user can be the sender or recipient of a conversation.
To fetch all conversations I must specify the sender_id and
recipient_id on the User model, but I can only specify one of the two
as shown below.
class User < ActiveRecord::Base
has_many :conversations, :foreign_key => 'sender_id'
end
Is there any way to specify two foreign keys?
Sure! You can have two separate associations between the same tables --
or follow JDevine's suggestion.
Thanks!
--
You received this message because you are subscribed to the Google
Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.