我正在尝试添加一个counter_cache,这个Rails 3迁移给我一个我似乎无法解决的错误.
迁移是
class AddItemsCountToStore < ActiveRecord::Migration
def self.up
add_column :stores, :items_count, :integer, :default => 0
Store.reset_column_information
Store.all.each do |store|
store.update_attribute :items_count, store.items.count
end
end
def self.down
remove_column :stores, :items_count
end
end
而错误是:
== AddItemsCountToStore:迁移========================================= - add_column( :stores,:items_count,:integer,{:default => 0}) - > 0.0680s rake aborted!发生错误,此以及所有后续迁移都已取消:
items_count被标记为只读C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/persistence.rb:115:在`update_attribute"
有任何想法吗?
我有以下一段代码
@user = User.find(params[:id])
if (@user.activation_status == "active")
#some code here
@user.update_attribute('activation_status' ,'inactive') # Line 44
#send mail to user that his account is Acivated
else
end
Run Code Online (Sandbox Code Playgroud)
有没有机会Line 44失败?因为数据库内存已满或网络出现故障.在那种情况下会发生什么?如果这会产生问题,那么避免它的更好方法是什么?update_attribute失败后会返回什么?
有替代品update_attribute吗?我知道您仍然可以在 Rails 3 中使用它,但是您会收到弃用消息。
我需要使用的原因update_attribute是因为我需要跳过验证但运行回调。
我发现这样做(并避免弃用消息)的唯一方法是简单地从update_attribute以下位置提取代码:
Object.send("#{attribute}=", value)
Object.save(:validate => false)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有另一种(正确的)方法来做到这一点。
谢谢。
有人可以告诉我如何将x天数添加到Nifi格式(“ yyyy-MM-dd”)的日期属性中。