Set non-database attribute for rails model without

2020-08-19 07:34发布

In PHP, I can set an attribute (that is not a column in database) to a model. E.g.(PHP code),

$user = new User;
$user->flag = true;

But in rails, when I set any attribute that doesn't exist in database, it will throw an error undefined method flag. There is attr_accessor method, but what will happen if I need about ten temp attributes?

5条回答
Root(大扎)
2楼-- · 2020-08-19 07:41

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.

查看更多
Viruses.
3楼-- · 2020-08-19 07:44

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
查看更多
叛逆
4楼-- · 2020-08-19 07:52

If you need temp attributes you can add them to the singleton object.

instance = Model.new

class << instance
  attr_accessor :name
end
查看更多
趁早两清
5楼-- · 2020-08-19 08:00

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.

查看更多
够拽才男人
6楼-- · 2020-08-19 08:05

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
查看更多
登录 后发表回答