Set non-database attribute for rails model without `attr_accessor`

As the guys explained, attr_accessor is just a quick setter and getter.

We can set our Model attr_accessor on record initializing to be a Ruby#Hash for example using ActiveRecord#After_initilize method so we get more flexibility on temporarily storing values (idea credit to this answer).

Something like:

class User < ActiveRecord::Base

  attr_accessor :vars

  after_initialize do |user|
    self.vars = Hash.new
  end

end

Now you could do:

user = User.new

#set
user.vars['flag'] = true

#get
user.vars['flag']
#> true

This is expected because it's how ActiveRecord works by design. If you need to set arbitrary attributes, then you have to use a different kind of objects.

For example, Ruby provides a library called OpenStruct that allows you to create objects where you can assign arbitrary key/values. You may want to use such library and then convert the object into a corresponding ActiveRecord instance only if/when you need to save to the database.

Don't try to model ActiveRecord to behave as you just described because it was simply not designed to behave in that way. That would be a cargo culting error from your current PHP knowledge.


but what will happen if I need about ten temp attributes?

#app/models/user.rb
class User < ActiveRecord::Base
   attr_accessor :flag, :other_attribute, :other_attribute2, :etc...
end

attr_accessor creates "virtual" attributes in Rails -- they don't exist in the database, but are present in the model.

As with attributes from the db, attr_accessor just creates a set of setter & getter (instance) methods in your class, which you can call & interact with when the class is initialized:

#app/models/user.rb
class User < ActiveRecord::Base
   attr_accessor :flag

   # getter
   def flag
     @flag
   end

   # setter
   def flag=(val)
     @flag = val
   end
end

All that attr_accessor does is add getter and setter methods which use an instance variable, eg this

attr_accessor :flag

will add these methods:

def flag
  @flag
end

def flag=(val)
  @flag = val
end

You can write these methods yourself if you want, and have them do something more interesting than just storing the value in an instance var, if you want.