If I have 3 models…
Post, Logo_Category and Logo
and 5 tables
posts, logos, logo_categories, logos_posts and logos_categories_posts
A post can have any number of logos from any number of logo_categories. I have started down the line with HABTM but I think I may have chosen the wrong path in terms of my schema.
Anyone have any idea what is the best way of associating these models?
Cheers
EDIT
Sorry guys, I thought I had solved this issue but…
In line with what Paul suggested I have now changed my associations to the following.
POST.erb
has_and_belongs_to_many :logos
has_many :logo_categories, :through => :logos
LOGO.erb
belongs_to :logo_category
has_and_belongs_to_many :posts
LOGO_CATEGORY.erb
has_many :logos
has_and_belongs_to_many :posts
and my tables are now…
posts (id) , logos (id), logos_posts (id, logo_id, logo_category_id),
logo_categories (id)
My post _form loads fine and all of the logos and logo_categories are loaded with the correct values and checked/unchecked (when editing a post) correctly.
I can check a new logo or uncheck an existing one and that records fine. However, if I alter a logo_category I get the following error!
Cannot modify association ‘Post#logo_categories’ because it goes
through more than one other association.
Any ideas?
For your table:
has one row in logo table with a unique logo_id
is linked to one logo_category row
with a post, a row is created in post_logos table. It has
two foreign-key columns: post_id and logo_id.
In ruby, each Post object could have an array or list of logo objects. Each Logo object composes one Logo_Category object (has one Logo_Category member or variable) .