Reputation: 505
Please excuse the confusing phrasing in the title. In my RoR project let's say I have it set up like this
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
and
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
I then have a categories_products table that connects them. This works fine but my problem is that a product
will only ever have one category
at a time and I'd of course like to do product.category
instead of having to deal with an array. How can I accomplish that?
Upvotes: 0
Views: 30
Reputation: 29224
A one-to-many representation is demonstrated in the rails guides like this:
class Category < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :category
end
Upvotes: 1