Reputation: 18895
I have the two following methods in class_controller
in my Rails 4-based app:
checkout(), checkin()
The operations to the database these two methods perform are opposite: checkout()
insert String value to the checkout
field/column, while the checkin()
removes the String value from the checkout
field/column.
The code in both methods are quite similar:
def checkout
@class = Class.where("id = ?", params[:class_id])
@class.update_all(":checkout" => "done blah blah")
end
def checkin
@class = Class.where("id = ?", params[:class_id])
@class.update_all(":checkout" => "") # set it empty or nil??
end
The call to checkout() mapped by a url works flawlessly, however, when the checkin() method throws the following error. Looks like the update_all
method behaves inconsistently:
PG::UndefinedColumn: ERROR: column ":checkout" of relation "classes" does not exist LINE 1: UPDATE "classes" SET ":checkout" = '' WHERE (id = '1') ^ : UPDATE "classes" SET ":checkout" = '' WHERE (id = '1')
Upvotes: 0
Views: 149
Reputation: 16012
Use either "checkout"
or :checkout
with update_all
.
@class.update_all(:checkout => "done blah blah")
Symbols in Ruby don't have to be quoted.
Upvotes: 1
Reputation: 107142
I am surprised that you say the first version works. It should also fail, if there isn't a column named :checkout
.
I would suggest to "fix" both versions to more common Ruby idioms:
def checkout
@class = Class.where(id: params[:class_id])
@class.update_all(checkout: 'done blah blah')
end
def checkin
@class = Class.where(id: params[:class_id])
@class.update_all(checkout: '') # set it empty or nil?
end
Upvotes: 1