ruby - Can not update ActiveRecord in rails console -


just experiement purpose, typed following commends rails console. queried arbitrary item , assign category_id , call "save". why, in result, nothing has changed (category_id of item still null)?

(some output has omitted clarity)

1.9.3-p327 :004 > = item.first item load (0.2ms)  select "items".* "items" limit 1 1.9.3-p327 :005 > i.category_id =1 1.9.3-p327 :006 > i.save (0.1ms)  begin transaction (0.1ms)  commit transaction => true  1.9.3-p327 :007 > => #<item id: 1, title: "near", price: 1000.0, photos: nil, created_at: "2013-07-31 15:19:24", updated_at: "2013-07-31 15:51:46", user_id: nil, category_id: nil, location_id: 1>  

also tried update_attributes

1.9.3-p327 :008 > i.update_attributes(:category_id => 1)    (0.1ms)  begin transaction    (0.1ms)  commit transaction => true  1.9.3-p327 :009 > => #<item id: 1, title: "near", price: 1000.0, photos: nil, created_at: "2013-07-31 15:19:24", updated_at: "2013-07-31 15:51:46", user_id: nil, category_id: nil, location_id: 1>  

------------------------------------------------edit ----------------------------

1.9.3-p327 :007 > i.price=50  => 50  1.9.3-p327 :008 > i.save    (0.1ms)  begin transaction    (0.3ms)  update "items" set "price" = 50.0, "updated_at" = '2013-07-31 21:28:33.643283' "items"."id" = 1    (229.6ms)  commit transaction  => true  

try attribute "price", works. say, rails prevent manually changes attributes end "_id", presumably protect foreign key?

can confirm this? or reproduce this?

------------------------------------edit again --------------------------------- model attached

class item < activerecord::base   attr_accessible :photos, :price, :title, :user_id, :category_id   attr_accessor :user_id, :category_id   belongs_to :user   belongs_to :category   belongs_to :location   end 

didn't see last update. yes, if use

attr_accessor :user_id, :category_id 

you can't change values in console, tried it

attr_accessor ruby method makes getter , setter, attr_accessible rails method allows pass in values mass assignment: new(attrs) or update_attributes(attrs).


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -