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
精彩评论