Forum: Ruby on Rails Many-to-many relationship with multiple models?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-03-29 23:44
I have a simple CMS I'm creating, and the primary model is the
"listing". A listing habtm (has_and_belongs_to_many) categories (up to
two) - allowing the listing to print its categories and a category to
print its listings. I also need subcategories, which are different from
categories in two ways: a listing can have as many subcategories (from
it's two categories) as it wishes, and subcategories are displayed
differently in both administration and display views.

So what I have is:

Class Listing < ActiveRecord::Base

  has_and_belongs_to_many :categories
  has_and_belongs_to_many :subcategories

  ...

end

Class Subcategory < ActiveRecord::Base

  has_and_belongs_to_many :listings
  belongs_to :categories

end

Class Category < ActiveRecord::Base

  has_and_belongs_to_many :listings
  has_many :subcategories

  ...

end

Is this allowed? I ask because I can't create a listing with
subcategories (from the admin UI) - the UI works fine, there are no
errors, but the new listing has no subcategories and the MySQL DB isn't
updated.

-Adam
This topic is locked and can not be replied to.