In the following hypothetical scenario, I''d like to associate Books and Clubs. Class Club < ActiveRecord::Base has_many :memberships has_many :readers, :through => :memberships #DOES NOT WORK #has_many :books, :through => :readers #DOES NOT ADD find_in_collection METHODS #has_many :books, :finder_sql => # %q{ # select distinct books.* # from books # join memberships on books.reader_id = memberships.reader_id # and memberships.club_id #{self.id} # order by books.read_at desc # } end Class Memberships < ActiveRecord::Base belongs_to :club belongs_to :reader end Class Reader < ActiveRecord::Base has_many :memberships has_many :books end Class Book < ActiveRecord::Base belongs_to :reader end If Memberships were simply a mapping table (clubs_readers) and class Club has_and_belongs_to_many :readers, I still couldn''t specify that class Club has_many :books, :through => :readers because you can''t associate through habtm. Unfortunately, associating books through readers in class Club does not work because readers is associated through memberships. Apparently, multiple levels of indirection is not supported. If you use finder_sql, all the find_in_collection methods are not added to the association proxy. Without which I won''t be able to call my_club.books.find(:all, :conditions => ''is_read = true'') or to do pagination for example. Does anyone have any idea how I can create this association without finder_sql and perhaps extending the association proxy? 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 -~----------~----~----~----~------~----~------~--~---
Bump On Jan 29, 11:22 am, gsterndale <gsternd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In the following hypothetical scenario, I''d like to associate Books > and Clubs. > > Class Club < ActiveRecord::Base > has_many :memberships > has_many :readers, :through => :memberships > > #DOES NOT WORK > #has_many :books, :through => :readers > > #DOES NOT ADD find_in_collection METHODS > #has_many :books, :finder_sql => > # %q{ > # select distinct books.* > # from books > # join memberships on books.reader_id > = memberships.reader_id > # and memberships.club_id > #{self.id} > # order by books.read_at desc > # } > end > > Class Memberships < ActiveRecord::Base > belongs_to :club > belongs_to :reader > end > > Class Reader < ActiveRecord::Base > has_many :memberships > has_many :books > end > > Class Book < ActiveRecord::Base > belongs_to :reader > end > > If Memberships were simply a mapping table (clubs_readers) and class > Club has_and_belongs_to_many :readers, I still couldn''t specify that > class Club has_many :books, :through => :readers because you can''t > associate through habtm. > > Unfortunately, associating books through readers in class Club does > not work because readers is associated through memberships. > Apparently, multiple levels of indirection is not supported. > > If you use finder_sql, all the find_in_collection methods are not > added to the association proxy. Without which I won''t be able to call > my_club.books.find(:all, :conditions => ''is_read = true'') or to do > pagination for example. > > Does anyone have any idea how I can create this association without > finder_sql and perhaps extending the association proxy? > > 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 -~----------~----~----~----~------~----~------~--~---
gsterndale wrote:> In the following hypothetical scenario, I''d like to associate Books > and Clubs.[...]> If Memberships were simply a mapping table (clubs_readers) and class > Club has_and_belongs_to_many :readers, I still couldn''t specify that > class Club has_many :books, :through => :readers because you can''t > associate through habtm. > > Unfortunately, associating books through readers in class Club does > not work because readers is associated through memberships. > Apparently, multiple levels of indirection is not supported. > > If you use finder_sql, all the find_in_collection methods are not > added to the association proxy. Without which I won''t be able to call > my_club.books.find(:all, :conditions => ''is_read = true'') or to do > pagination for example. > > Does anyone have any idea how I can create this association without > finder_sql and perhaps extending the association proxy? > > Thanks!Your right, the multi-pass :through statements are not supported. It''s messy enough as it is with just a single through. I really don''t have a good answer, and I think the silence may be because no one else has come up with one either. But this has been sitting here a while, and I thought it would be polite to at least let you know I looked at it. My only thought is to write a few functions in Club like ''read_books'' or ''unread_books'' and drop back to SQL under the covers, but that is not a good solution, just a hack. -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks Starr. I appreciate the thought. I''ll update this thread if I find a solution. Thanks again. On Feb 2, 2:09 am, Starr Trader <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> gsterndale wrote: > > In the following hypothetical scenario, I''d like to associate Books > > and Clubs. > [...] > > If Memberships were simply a mapping table (clubs_readers) and class > > Club has_and_belongs_to_many :readers, I still couldn''t specify that > > class Club has_many :books, :through => :readers because you can''t > > associate throughhabtm. > > > Unfortunately, associating books through readers in class Club does > > not work because readers is associated through memberships. > > Apparently, multiple levels of indirection is not supported. > > > If you use finder_sql, all the find_in_collection methods are not > > added to the association proxy. Without which I won''t be able to call > > my_club.books.find(:all, :conditions => ''is_read = true'') or to do > > pagination for example. > > > Does anyone have any idea how I can create this association without > > finder_sql and perhaps extending the association proxy? > > > Thanks! > > Your right, the multi-pass :through statements are not supported. It''s > messy enough as it is with just a single through. I really don''t have a > good answer, and I think the silence may be because no one else has come > up with one either. But this has been sitting here a while, and I > thought it would be polite to at least let you know I looked at it. My > only thought is to write a few functions in Club like ''read_books'' or > ''unread_books'' and drop back to SQL under the covers, but that is not a > good solution, just a hack. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---