nj
2008-Feb-05 14:09 UTC
has_many association where the source model can be in several columns
Hello all, I have an application dealing with User and Transaction objects, each transaction going from a source user to a target user. I have defined the following associations in user.rb : has_many :transactions_as_source, :class_name => "Transaction", :as => :source has_many :transactions_as_target, :class_name => "Transaction", :as => :target and this is working fine, but I''m looking for a way to define "has_many :transactions" to get all the transactions having the user as a source *OR* a target, i.e. something like: has_many :transactions, :finder_sql => ''SELECT * FROM transactions WHERE source_id = #{id} OR target_id = #{id}'' but without resorting to finder_sql (as with this definition I cannot cascade additional queries such as current_user.transactions.sum(:amount) or current_user.transactions.find(...). Thanks, Nicolas --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Harry Seldon
2008-Feb-26 20:52 UTC
Re: has_many association where the source model can be in se
Hum, it looks like you are about to enjoy the has_many through polymorphism troubles... Check here : http://blog.hasmanythrough.com/2006/4/3/polymorphic-through My only personal advice will be : be VERY careful with each letter (specifically the "s") you will use because if it does not work Rails won''t say a word. H nj wrote:> Hello all, > > I have an application dealing with User and Transaction objects, each > transaction going from a source user to a target user. > > I have defined the following associations in user.rb : > > has_many :transactions_as_source, :class_name => "Transaction", :as > => :source > has_many :transactions_as_target, :class_name => "Transaction", :as > => :target > > and this is working fine, but I''m looking for a way to define > "has_many :transactions" to get all the transactions having the user > as a source *OR* a target, i.e. something like: > > has_many :transactions, :finder_sql => ''SELECT * FROM transactions > WHERE source_id = #{id} OR target_id = #{id}'' > > but without resorting to finder_sql (as with this definition I cannot > cascade additional queries such as > current_user.transactions.sum(:amount) or > current_user.transactions.find(...). > > Thanks, > > Nicolas-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---