Lp
2009-Mar-03 19:50 UTC
Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
Hello,
I have 2 models: User and Message
Message:
belongs_to :sender, :class_name=>''User'',
:foreign_key=>''sender_id''
belongs_to :receiver, :class_name=>''User'',
:foreign_key=>''receiver_id''
// MESSAGE-MIGRATION START
create_table :messages do |t|
t.integer :sender_id
t.integer :receiver_id
t.string :subject
t.text :content
t.timestamps
end
// MIGRATION END
User:
has_many :sent_messages, :class_name => "Message", :foreign_key
=>
"sender_id"
has_many :received_messages, :class_name => "Message",
:foreign_key
=> "receiver_id"
I want to access User.messages, because I need it for the destroy-
action -> User.messages.find(params[:id])
Can anyone help me?
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Lp
2009-Mar-03 19:57 UTC
Re: Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
Hmmm...
It worked with:
has_many :friendships, :class_name => "Friendship", :foreign_key
=>
"sender_id = #{self.id} or friendships.receiver_id = #{self.id} AND 1"
But thats not very nice...!!??!!?! Is there a better solution??
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-03 19:58 UTC
Re: Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
On Mar 3, 7:50 pm, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I want to access User.messages, because I need it for the destroy- > action -> User.messages.find(params[:id]) >You want a single association that covers both sent and received messages ? AFAIL there is no way to get an association to do that (although you could construct a named scope that did). Fred> Can anyone help me? > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
Lp
2009-Mar-03 20:02 UTC
Re: Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
Hm, I tried it with named scopes, but User.rb: named_scope :messages, :join => :messages, :conditions = ["sender_id = ? or receiver_id = ?", self.id, self.id] returns the collection of USERS -> not messages! Or is there chance to get the messages? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-03 20:15 UTC
Re: Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
On Mar 3, 8:02 pm, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hm, I tried it with named scopes, but > > User.rb: > > named_scope :messages, :join => :messages, :conditions = ["sender_id > = ? or receiver_id = ?", self.id, self.id] > > returns the collection of USERS -> not messages! > > Or is there chance to get the messages?You''d have to put the scope on Message class if you want it to return messages ie named_scope :for_user, lambda {|u| :conditions => ["sender_id = ? or received_id = ?", u,u]} Then Message.for_user(bob) returns a scope for bob''s messages. You could define a messages method on user to make this flow slightly nicer ie def messages Message.for_user(self) end Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
Lp
2009-Mar-03 20:22 UTC
Re: Multiple foreignkeys for same model with different meaning (User.received_messages / User.sent_messages/ -> User.messages?)
Cool, thx! Nice idea... --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---