我有一个数据库项和其中的两个项。他们有一个名为"popularity“的列,我将其设置为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如何通过code\console更改并保存该值?我试过了
Item.find(1).popularity = 1
Item.save但这并没有救我的瓦尔。怎么了?
发布于 2012-09-08 17:56:56
以下是解决方案
item = Item.find(1)
item.popularity = 1
item.save发布于 2012-09-08 17:57:33
item = Item.first
item.popularity = 1
item.save发布于 2020-07-23 18:39:10
Item.update(1, popularity: 1)如果不存在,
有关更新attributes https://zaiste.net/posts/rails-activerecord-updating-attributes-object-fields/的详细信息
https://stackoverflow.com/questions/12329687
复制相似问题