Adding more data on saving 'has_many :through' associated records by keeping use of the “RoR magical\automatic way”

别说谁变了你拦得住时间么 提交于 2019-12-25 10:53:08

问题


I am using Ruby on Rails 3.0.7 and I followed this post about handling an "has_many :through => checkboxes" in which, in order to create user-group relationship records for membership purposes, is just passed a group_ids parameter (that is an array of id values) from check box input fields to the @user.save method. Using that code all works good in a "RoR magical\automatic way" (RoR set properly user_id values in the related memberships database table).

However, on saving, I would like to add some extra information to Membership records. That is, (following that post content) when it creates the associated records in the memberships database table (which database table has id, user_id and group_id columns) I would like to save an extra description information (which, for example, can be represented by a description column in that memberships table).

Is it possible to accomplish that by keeping use of the "RoR magical\automatic way"?


回答1:


I would use the ActiveRecord Callbacks to achieve this :

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html



来源:https://stackoverflow.com/questions/6582488/adding-more-data-on-saving-has-many-through-associated-records-by-keeping-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!