My Category model :
class Category < ActiveRecord::Base
has_many :item_categoryships
has_many :items, :through => :item_categoryships
end
My Item model :
class Item < ActiveRecord::Base
has_many :item_categoryships
has_many :categories, class_name: 'ItemCategoryship', foreign_key: 'category_id', :through => :item_categoryships
end
My ItemCategoryship model:
class ItemCategoryship < ActiveRecord::Base
belongs_to :item
belongs_to :category
end
And in views/items/edit.html.erb, I wrote simple form code like this:
<%= simple_form_for(@item) do |f| %>
<%= f.association :categories, collection: @categories, as: :check_boxes %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
The @categories above, I wrote this in controller:
@categories = current_user.categories
But I hit a problem, they can't save to database!!
I couldn't find out what the problem was. Please help me.... Thanks you all.