I have an database items and two items in it. They have column named "popularity", that I set to 0.
class Item < ActiveRecord::Base
attr_accessible .. :popularity, ..
before_create :default_values
def default_values
if self.popularity.nil? == true || self.popularity.blank? == true || self.popularity.class != Integer
self.popularity = 0
end
end
How to change this value via code\console and save it? I tried
Item.find(1).popularity = 1
Item.save
But it didn't save my val. Whats wrong?
If no 'value' is specified in @ModelAttribute then the returned type is used as the attribute name. This annotation is used to populated common model attributes for multiple request handlers.
As the introductory paragraph revealed, @ModelAttribute can be used either as a method parameter or at the method level. When the annotation is used at the method level it indicates the purpose of that method is to add one or more model attributes.
This one will do the replacement of the values in the newly created attribute. By using the Arcade code expression right at the bottom. This uses the creation of a variable and a conditional "if" statement to find and replace values accordingly.
Overview One of the most important Spring-MVC annotations is the @ModelAttribute annotation. The @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view.
here is the solution
item = Item.find(1)
item.popularity = 1
item.save
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With