0

I have two models User and Category. Consider the following code

class User < ActiveRecord::Base
    has_and_belongs_to_many :categories
    accepts_nested_attributes_for :categories, :allow_destroy => true
    alias_method :categories=, :categories_attributes=
end

and

class Category < ActiveRecord::Base
    has_and_belongs_to_many :users
end

I want to create categories when it is not already present in the category table. If the category is already present in the table then i need to refer the id of the category to the user in the join table. And consider i need to add a field say type in the join table along with the reference where i need to add the type of the category.

Say for example

user table:

1, sample_user
2, test_user

and

category table:

1, category1
2, category2

and

categories_users:

category_id               user_id       type
      1                     1            type1
      2                     1            type2
      1                     2            type2
      1                     2            type1

And when getting the category of users i need to get the category object along with the type of the category within the category object.

How can i do this? please help me

logesh
  • 2,572
  • 4
  • 33
  • 60
  • possible duplicate of [How can i use accepts nested attributes with the HABTM?](http://stackoverflow.com/questions/18575039/how-can-i-use-accepts-nested-attributes-with-the-habtm) – gabrielhilal Sep 03 '13 at 18:28

1 Answers1

2

If you want attributes on the join table in a HABTM association, you should probably look into making the join table a separate model, and using has_many :through instead.

In this case, that would yield something like this:

class User < ActiveRecord::Base
  has_many :categories, through: :user_categories
end

class Category < ActiveRecord::Base
  has_many :users, through: :user_categories
end

class UserCategory < ActiveRecord::Base # or whatever you want to call it
  belongs_to :user
  belongs_to :category
end
Frost
  • 11,121
  • 3
  • 37
  • 44
  • Ok. And how about the nested attributes. can i be able to add it to the categories table and refer it in the user_categories table automatically as did by the HABTM? Also i want to know how can i use the category id as reference if the category is already present and also how can the extra attributes be added to the join table as if the reference were stored automatically? – logesh Sep 03 '13 at 09:19
  • As for the nested attributes, it might be possible to use `accepts_nested_attributes_for`. You should probably look into that. – Frost Sep 03 '13 at 09:21
  • I think i can use "accepts_nested_attributes_for :categories" but will the reference be stored in the user_categories table by itself and please tell me how can i add values to that extra attributes in the join table when sending the data for categories? – logesh Sep 03 '13 at 09:24
  • Thankyou. It took me some time to understand. Now it is clear about using through but please tell me how i can store the other attribute in the join table. – logesh Sep 04 '13 at 04:54