class Company < ActiveRecord::Base belongs_to :category, :class_name => ''CompanyCategory'' ... end On 5/28/05, Robert Mannl <ro-x0ewNDjvrP1m7dl9lghbdg@public.gmane.org> wrote:> Hi! > > I wanted to start an discussion about model naming, and ask you how you > do it. There''s one problem that puts me a little bit off when designing > a Rails app. > > Say we''re coding a yellow-pages type of website. We have Companies, and > we have Categories (Grocery, Hardware store, etc..). We also want to > have classifieds on the website. We''ll call the model for the > classifieds "Ad". We also have Categories for the Classifieds, as we > want things like "Looking for", and "Selling", etc. > > See the Problem? We have the following models: > > Company and Category > and > Ad and Category > > Two Category models. > > Ok, we rewrite it to > Company and CompanyCategory > and > Ad and AdCategory > > That''s the way I do it. But it''s just ugly: > > <%= @company.company_category.name %> > > <%= @company.category.name %> > would be much more beautiful. > > > Any ideas? How do you do it? > > > Rob > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''m assuming that a Company is different from an Ad, and a CompanyCategory is different from an AdCategory. In that case, you''d have class Company < ActiveRecord::Base belongs_to :category, :class_name => ''CompanyCategory'' ... end class Ad < ActiveRecord::Base belongs_to :category, :class_name => ''AdCategory'' ... end and of course the CompanyCategory and AdCategory classes. Take a look at the AR docs. Basically, all you''re doing here is saying, "Company has a relationship to a category, find it with category_id, but make sure you treat it as a CompanyCategory." Likewise with the Ad and AdCategory. On 5/28/05, Robert Mannl <ro-x0ewNDjvrP1m7dl9lghbdg@public.gmane.org> wrote:> Ok, but I can''t have two models: > > class Company < ActiveRecord::Base > belongs_to :category, :class_name => ''CompanyCategory'' > ... > end > > and > > class Company < ActiveRecord::Base > belongs_to :category, :class_name => ''AdCategory'' > ... > end > > > can I? > > >
Hi! I wanted to start an discussion about model naming, and ask you how you do it. There''s one problem that puts me a little bit off when designing a Rails app. Say we''re coding a yellow-pages type of website. We have Companies, and we have Categories (Grocery, Hardware store, etc..). We also want to have classifieds on the website. We''ll call the model for the classifieds "Ad". We also have Categories for the Classifieds, as we want things like "Looking for", and "Selling", etc. See the Problem? We have the following models: Company and Category and Ad and Category Two Category models. Ok, we rewrite it to Company and CompanyCategory and Ad and AdCategory That''s the way I do it. But it''s just ugly: <%= @company.company_category.name %> <%= @company.category.name %> would be much more beautiful. Any ideas? How do you do it? Rob
Ok, but I can''t have two models: class Company < ActiveRecord::Base belongs_to :category, :class_name => ''CompanyCategory'' ... end and class Company < ActiveRecord::Base belongs_to :category, :class_name => ''AdCategory'' ... end can I?