Active record error when migrating from rails 2.3 to rails 3.1 -


i migrating rails 2.3 application rails 3.1, getting error when try save record. not happening earlier , not sure reason, appreciated, thanks.

here previews of 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 error message log when try save

**

activerecord::recordinvalid: validation failed: items order can't blank     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/validations.rb:56:in `save!'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/attribute_methods/dirty.rb:33:in `save!'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:246:in `block in save!'     /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'     /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'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:208:in `transaction'     /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'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/transactions.rb:246:in `save!'     (irb):16     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands/console.rb:45:in `start'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands/console.rb:8:in `start'     /users/branch/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.1.12/lib/rails/commands.rb:40:in `<top (required)>'     script/rails:6:in `require'     script/rails:6:in `<main>' 1 

**

when try save order via console. primary key getting saved nil.

o = st.orders.new(:store_order_id => "1m",:date_time => time.now)

here see when

o.save  #<order id: nil, store_order_id: "12121m", date_time: nil> 

below preview order controller create code

def create @order = @store.orders.new(params[:order]) @order.save  end 

you issue can't this:

validates_presence_of :order_id 

in item model. reason simple: order id created after validation , doesn't exist.

there couple of things do:

  1. add inverse relationships
  2. change validation check presence of "order", not "order_id"

here's 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 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -