0
我試圖保存用戶和類別之間的新HABTM關係。Rails 2:保存新的HABTM關係
我的用戶模型:
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
end
我的類別模型:
class Category < ActiveRecord::Base
has_and_belongs_to_many :users
end
我已經連接表設置正確。問題是,我無法更改用戶控制器上的任何內容,只能在用戶視圖上進行更改。因此,在用戶看來,有一個collection_select:
<%= f.collection_select :category_ids, Category.all, :id, :title,
{ :selected => @user.category_ids },
{ :multiple => true, :name => 'user[category_ids][]' }
-%>
我可以correclty選擇我的用戶屬於類,但是當我點擊提交,所有其他屬性被保存,除了類別,這是不更新連接表。
如果我檢查參數,可以我可以看到category_ids正在正確傳遞:
"user"=>{"category_ids"=>["2", "4"]
但用戶控制器上我不能用它做任何事情,因爲我不能亂與代碼。有沒有辦法自動更新連接表,或者我必須手動更新它?
PS - 我不能混淆用戶控制器,因爲我實際上正在爲Redmine構建一個插件。
謝謝!