Hiya everybody! Is there any reason I can''t do this: class Category < ActiveRecord::Base ... end class NameCategory < Category belongs_to_and_has_many :names ... end class ContactCategory < Category belongs_to_and_has_many :contacts ... end class Contact < ActiveRecord::Base belongs_to_and_has_many :contact_category end Julian
On 29.8.2005, at 12.41, Julian Leviston wrote:> Hiya everybody! > > Is there any reason I can''t do this: > > class Category < ActiveRecord::Base > ... > end > > class NameCategory < Category > belongs_to_and_has_many :names > ... > end > > class ContactCategory < Category > belongs_to_and_has_many :contacts > ... > end > > class Contact < ActiveRecord::Base > belongs_to_and_has_many :contact_category > endYes. There is no method called belongs_to_and_has_many. You probably meant has_and_belongs_to_many ;-) You also need to pluralise the symbol: has_and_belongs_to_many :contact_categories. Other than that, I don''t see why it wouldn''t work. //jarkko> > Julian > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Julian Leviston wrote:> Is there any reason I can''t do this: > belongs_to_and_has_many :namesTry: has_and_belongs_to_many Also, if you haven''t discovered it yet, this is very useful: http://api.rubyonrails.com/ Cheers, ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Oh... thanks man! I''ll go check that out today! :-P By the way, I wasn''t referring to the HABTM, I was referring to the inheriting of database tables through AR baseclasses. Julian. On 30/08/2005, at 7:23 PM, Dave Silvester wrote:> Julian Leviston wrote: > >> Is there any reason I can''t do this: >> belongs_to_and_has_many :names >> > > Try: has_and_belongs_to_many > > Also, if you haven''t discovered it yet, this is very useful: > > http://api.rubyonrails.com/ > > Cheers, > > ~Dave > > -- > > Dave Silvester > Rent-A-Monkey Website Development > Web: http://www.rentamonkey.com/ >
On 8/29/05, Julian Leviston <julian-AfxEtdRqmE/tt0EhB6fy4g@public.gmane.org> wrote:> Hiya everybody! > > Is there any reason I can''t do this: > > class Category < ActiveRecord::Base > ... > end > > class NameCategory < Category > belongs_to_and_has_many :names > ... > end > > class ContactCategory < Category > belongs_to_and_has_many :contacts > ... > end > > class Contact < ActiveRecord::Base > belongs_to_and_has_many :contact_category > end > > JulianNone at all. You might get some error messages because of the tables it''s trying to use, however. Anytime I do this, I specify a single table anyway, so I''m not sure what the default behavior is. For instance, will NameCategory want to use a names_name_categories join table? Or will it want categories_names since NameCategory is subclassed from Category? It really doesn''t matter anyway. I prefered a single table in my own app. Just set up the files, run script/console, and watch the log as you play around with the objects. Just create a new category, new contact or name, and add it to see what happens. -- rick http://techno-weenie.net
On Mon, 2005-08-29 at 19:41 +1000, Julian Leviston wrote:> Hiya everybody! > > Is there any reason I can''t do this: > > class Category < ActiveRecord::Base > ... > end > > class NameCategory < Category > belongs_to_and_has_many :names > ... > endThere isn''t a belongs_to_and_has_many and it wouldn''t make sense. In your above example, a belongs_to relationship on :names would be wrong since you can only belong_to one object. Remember that a belongs_to relationship has the foreign key in it''s table/record. A has_many relationship has the foreign key in the other table and therefore the :names would make sense, but you can''t have both relations using the same association_id as it would then be ambiguous as to which function you wanted. Even with that explanation, I doubt it expresses what you want it to do anyways. I doubt you really want NameCategory to derive from Category. I think you where looking for a way to short cut the one line of code to make the link back to Category. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>