Here''s how my associations map: class Journal < ActiveRecord::Base has_many :music_journal_entries has_many :film_journal_entries end class FilmJournalEntry < ActiveRecord::Base belongs_to :journal end class MusicJournalEntry < ActiveRecord::Base belongs_to :journal end Both "entries" tables have correct foreign keys set-up and contain records that map correctly when querying their values through Journal. I just want to be able to pull in all of the film_journal_entries and music_journal_entries buy running an AR query through Journal. I have tried things like: all_entries = Journal.all :joins => [:music_journal_entries, :film_journal_entries] and some other things utilizing :include, but I still haven''t got it. How would I do this? Thanks!! Elliott G -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 April 2010 19:00, elliottg <x@simplecircle.net> wrote:> Both "entries" tables have correct foreign keys set-up and contain > records that map correctly when querying their values through > Journal. > > I just want to be able to pull in all of the film_journal_entries and > music_journal_entries buy running an AR query through Journal. >The easiest way is just: all_entries = music_journal_entries + film_journal_entries But it might be worth remodelling your DB a little. It looks to me like a good candidate for STI to have a single "journal_entries" table, and then a type of MusicJournalEntry and a type of FilmJournalEntry (of course, if they''ve got loads of different properties, you might not want to use STI, but maybe a polymorphic model would help instead). That would keep all of a journal''s entries in one table: class JournalEntry < ActiveRecord::Base belongs_to :journal end class FilmJournalEntry < JournalEntry end class MusicJournalEntry < JournalEntry end All you need to do to support this is to ensure there''s a string field in the "journal_entries" table called "type" - Rails handles the rest. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Thanks for the input Michael. I''ll rethink my data structure a bit. EG On Apr 9, 2:40 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 April 2010 19:00, elliottg <x...-+PdThUrr9bOXUBVsB0ZYTw@public.gmane.org> wrote: > > > Both "entries" tables have correct foreign keys set-up and contain > > records that map correctly when querying their values through > > Journal. > > > I just want to be able to pull in all of the film_journal_entries and > > music_journal_entries buy running an AR query through Journal. > > The easiest way is just: > > all_entries = music_journal_entries + film_journal_entries > > But it might be worth remodelling your DB a little. It looks to me > like a good candidate for STI to have a single "journal_entries" > table, and then a type of MusicJournalEntry and a type of > FilmJournalEntry (of course, if they''ve got loads of different > properties, you might not want to use STI, but maybe a polymorphic > model would help instead). > That would keep all of a journal''s entries in one table: > > class JournalEntry < ActiveRecord::Base > belongs_to :journal > end > > class FilmJournalEntry < JournalEntry > end > > class MusicJournalEntry < JournalEntry > end > > All you need to do to support this is to ensure there''s a string field > in the "journal_entries" table called "type" - Rails handles the rest.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Elliott Golden wrote:> Thanks for the input Michael. > > I''ll rethink my data structure a bit. > > EGYour could use Single table inheritance or polymorphic set up if you are concerned about table getting too large in inheritance.... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.