开发者

What is update method for Rails Associations?

This is so simple that it's ridiculous I couldn't find any information about this anywhere including API docs and Rails source code:

I have a :belongs_to association and I've come to understand the normal model methods you call in the controller when you DON'T have an association are slightly different than the ones when you DO.

For example, I've got my association working fine for the create controller action:

@user = current_user
@building = Building.new(params[:building])

respond_to do |format|
   if @user.buildings.create(params[:building])
# et cetera

but I can't find docs on how update works:

@user = current_user
@building = @user.buildings.find(params[:id])

respond_to do |format|
  if @user.buildings.update(params[:buildin开发者_StackOverflowg])
# et cetera

Using the update method gives the error "wrong number of arguments (1 for 2)" and I can't figure out what arguments are supposed to be sent.


Use update_attributes:

@user = current_user
@building = @user.buildings.find(params[:id])

respond_to do |format|
  if @building.update_attributes(params[:building])
     #...
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜