开发者

Bulk update has_many relationship model from belongs_to model

class Project < ActiveRecord::Base
  has_many :pages
  attr_accessible :name, :class_name, :content, :style

  def bulk_update_pages(attributes)
    for a in attributes do
      pages.find(a['id'].to_i).update_attributes(a) if pages.exists?(a['id'])
    end
  end

end

class Page < Act开发者_如何学JAVAiveRecord::Base
  belongs_to :project
end

Is it a good way to bulk update pages from project ? Project.find(session[:ProjectId]).bulk_update_pages(params)


I think you'd be better off using a nested form:
http://railscasts.com/episodes/196-nested-model-form-part-1


Try setting the association to
has_many :pages, :autosave => true

"If you set the :autosave option to true, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object" Rails guide 4.1.2

http://guides.rubyonrails.org/association_basics.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜