It''s been a long time since I did a data-driven app, so sorry if this is a trivial question. I''m doing a database to drive an Internet radio station with prerecorded shows. Each :show has_and_belongs_to_many :djs. Each :show is also reviewed by a second DJ before airing. Further, each :show is made up of :tracks which are performed by :artists, for whom (if they''re not signed to a label) we need to keep the same type of info as DJs - contact, radio release forms, etc. If this weren''t in Rails, I''d probably have created a table of "people", and a show would have a dj_id and a reviewer_id, and a track would have an artist_id, and all would go to the people database, and I''d know how the relations worked because I''d have a design doc. But that''s going to break all the nice reflection that''s done for me, not to mention the magical join tables of djs_shows, reviewers_shows, etc. What''s the Rails Way to do this? -- Jay Levitt | Wellesley, MA | I feel calm. I feel ready. I can only Faster: jay at jay dot fm | conclude that''s because I don''t have a http://www.jay.fm | full grasp of the situation. - Mark Adler
Here''s what seems right to me: -- model Person < ActiveRecord::Base end model Artist < Person has_many :songs end model DJ < Person has_and_belongs_to_many :shows end model Reviewer < Person has_many :reviewed_shows, {:class_name=>:show} #not sure about syntax here end model Show < ActiveRecord::Base has_and_belongs_to_many :djs has_and_belongs_to_many :songs belongs_to :reviewer end model Song < ActiveRecord::Base belongs_to :artist has_and_belongs_to_many :songs end -- This would require tables: persons, songs, shows, One problem is that a list of DJ''s (DJ.find_all, for example) will possibly find non-DJ''s in there. A solution for this might be overriding find_all in some way to filter out non-DJ''s/non-Artists, and you either have some flags (persons.is_dj) or a cross-reference table of roles you can check. Am I right? -d, only slightly less of a rails newb On Wed, 30 Mar 2005 13:28:01 -0500, Jay Levitt <jay-news-WxwZQdyI2t0@public.gmane.org> wrote:> It''s been a long time since I did a data-driven app, so sorry if this is > a trivial question. > > I''m doing a database to drive an Internet radio station with prerecorded > shows. Each :show has_and_belongs_to_many :djs. Each :show is also > reviewed by a second DJ before airing. Further, each :show is made up > of :tracks which are performed by :artists, for whom (if they''re not > signed to a label) we need to keep the same type of info as DJs - > contact, radio release forms, etc. > > If this weren''t in Rails, I''d probably have created a table of "people", > and a show would have a dj_id and a reviewer_id, and a track would have > an artist_id, and all would go to the people database, and I''d know how > the relations worked because I''d have a design doc. But that''s going to > break all the nice reflection that''s done for me, not to mention the > magical join tables of djs_shows, reviewers_shows, etc. > > What''s the Rails Way to do this? > > -- > Jay Levitt | > Wellesley, MA | I feel calm. I feel ready. I can only > Faster: jay at jay dot fm | conclude that''s because I don''t have a > http://www.jay.fm | full grasp of the situation. - Mark Adler > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Wed, 30 Mar 2005 16:37:54 -0500, Dev Purkayastha <dev.purkayastha-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> model Song < ActiveRecord::Base > belongs_to :artist > has_and_belongs_to_many :songs > endWhy does a song have many songs? Typo or am I missing something?> One problem is that a list of DJ''s (DJ.find_all, for example) will > possibly find non-DJ''s in there. A solution for this might be > overriding find_all in some way to filter out non-DJ''s/non-Artists, > and you either have some flags (persons.is_dj) or a cross-reference > table of roles you can check.There''s a way around this that I read in the wiki once, but I don''t remember how to do it. I think if you add a table field "Class" and set it to values like "Dj" or "Reviewer" or whatever, then rails'' find_all will automatically know only which records to extract, and better yet if you use custom SQL to extract all people anyway, it''ll instantiate them properly as their Dj/Artist/Reviewer classes instead of just making them all into People classes. Though I forget the specific details of how to do that, search the wiki for it. -- Urban Artography http://artography.ath.cx
> This would require tables: persons, songs, shows,people actually> > One problem is that a list of DJ''s (DJ.find_all, for example) will > possibly find non-DJ''s in there. A solution for this might be > overriding find_all in some way to filter out non-DJ''s/non-Artists, > and you either have some flags (persons.is_dj) or a cross-reference > table of roles you can check. > > Am I right? >Not quite. All you need is to add a "type" column to the people table. Rails will use single table inheritance. A Person.find_all will find all types of people stored. DJ.find_all will only return DJs. This will also work fine with has_many relations to different kinds of people. Nifty huh? -- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
On Wed, 30 Mar 2005 17:42:13 -0500, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Not quite. All you need is to add a "type" column to the people table. > Rails will > use single table inheritance.Ah, that''s what I was thinking of. Thanks for clarifying. -- Urban Artography http://artography.ath.cx
In article <9963c1bd050330144233e15cc1-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>, tobias.luetke- Re5JQEeQqe8AvxtiuMwx3w-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says...> Not quite. All you need is to add a "type" column to the people table. > Rails will > use single table inheritance. A Person.find_all will find all types of > people stored. DJ.find_all will only > return DJs.Wow!! That is EXACTLY what I need. A slight OO spice on a RDBMS-flavored database. I knew I loved Rails. -- Jay Levitt | Wellesley, MA | I feel calm. I feel ready. I can only Faster: jay at jay dot fm | conclude that''s because I don''t have a http://www.jay.fm | full grasp of the situation. - Mark Adler