Models used
class Car < ActiveRecord::Base
attr_accessor :model,:edition
attr_accessible :model,:edition
has_many :wheels
end
class Wheel < ActiveRecord::Base
attr_accessor :type,:tubeless
attr_accessible :type,:tubeless
belongs_to :car
end
Create operation
irb(main):009:0> bnz = Car.create :model=>"BENZ" , :edition=>2011
=> #<Car id: 5, model: nil, edition: nil, created_at: "2011-08-26 09:08:08", upd
ated_at: "2011-08-26 09:08:08">
Problem:
The above creates record successfully but my :model and :edition values are not sent to database. only id,created_at,updated_at values are getting created in database.
Update operation
irb(main):010:0> fifth = Car.find(5)
irb(main):011:0> fifth.update_attributes(:edition=>2011, :model=>"BENZ")
=> true
irb(main):012:0> fifth.update_attributes!(:edition=>2011, :model=>"BENZ")
=> true
irb(main):013:0> fifth.update_attributes!(:edition=>2012, :model=>"BENZ")
=> true
irb(main):014:0>
Problem:
Nothing is happening here. In developement.log only select statements are fired, no update statement is fired
I’m using sqlite db. Using sqlite manager firefox addon to view the db tables.
New to rails, Thanks for help.
You should not use
attr_accessoras that is defining virtual attributes, overriding the setters uses for the fields. Remove those lines using it from your models and it should be fine.