In a Ruby on Rails model class:
class DeliveryItem < ActiveRecord::Base
attr_accessible :state
end
item=DeliveryItem.new
item.instance_variable_set "@name","apple"
NoMethodError: undefined method 'name' for #
How do I make this work?
In a Ruby on Rails model class:
class DeliveryItem < ActiveRecord::Base
attr_accessible :state
end
item=DeliveryItem.new
item.instance_variable_set "@name","apple"
NoMethodError: undefined method 'name' for #
How do I make this work?
The error means you don't have a name attribute for DeliveryItem. This means that there's no setter method for name or you don't have a name column. So to solve this issue, you have to either
name column orname orattr_accessor or attr_writer to do the work for youadd the following to your model
class DeliveryItem < ActiveRecord::Base
attr_accessor :name
end
then you should be able to use your code
item = DeliveryItem.new
item.instance_variable_set "@name","apple"
# or just use item.name=
item.name = 'apple'