[英]Active record error when migrating from rails 2.3 to rails 3.1
I am migrating from a rails 2.3 application to Rails 3.1, i am getting this error when i try to save a record. 我正在从Rails 2.3应用程序迁移到Rails 3.1,当我尝试保存记录时出现此错误。 This was not happening earlier and i am not sure what could be the reason, any help would be appreciated, Thanks.
这是较早发生的,我不确定是什么原因,感谢您的帮助。
Here are previews of my models 这是我的模型的预览
** **
class Store < ActiveRecord::Base
has_many :orders
end
** **
** **
class Order < ActiveRecord::Base
has_many :items, :dependent => :delete_all
end
** **
** **
class Item < ActiveRecord::Base
belongs_to :order
validates_presence_of :order_id
end
** **
Below is error message log when i try to save 以下是我尝试保存时的错误消息日志
** **
ActiveRecord::RecordInvalid: Validation failed: Items order can't be blank
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/validations.rb:56:in `save!'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:246:in `block in save!'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/connection_adapters/abstract/database_statements.rb:194:in `transaction'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:208:in `transaction'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:246:in `save!'
from (irb):16
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands/console.rb:45:in `start'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands/console.rb:8:in `start'
from /Users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1
** **
When i try to save Order via console. 当我尝试通过控制台保存订单时。 the primary key is getting saved as nil.
主键被保存为nil。
o = st.orders.new(:store_order_id => "1M",:date_time => Time.now)
here is what i see when i 这是我什么时候看到的
o.save
#<Order id: nil, store_order_id: "12121M", date_time: nil>
below is a preview my Order Controller Create code 下面是我的订单控制器创建代码的预览
def create
@order = @store.orders.new(params[:order])
@order.save
end
You issue is that you can't do this: 您的问题是您不能这样做:
validates_presence_of :order_id
in the Item model. 在项目模型中。 The reason is simple: the Order id is created after validation and thus doesn't exist.
原因很简单:订单ID是在验证后创建的,因此不存在。
There are a couple of things to do: 有几件事情要做:
Here's the code: 这是代码:
class Order < ActiveRecord::Base
has_many :items, :inverse_of => :order, :dependent => :delete_all
end
class Item < ActiveRecord::Base
belongs_to :order, :inverse_of => :items
validates_presence_of :order
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.