简体   繁体   English

attr_accessor 默认值

[英]attr_accessor default values

I'm using rails and I want to make it so that attr_accessor:politics is set, by default, to false.我正在使用 rails,我希望将attr_accessor:politics默认设置为 false。

Does anyone know how to do this and is able to explain it in simple terms for me?有谁知道如何做到这一点并且能够为我简单地解释它?

Rails has attr_accessor_with_default so you could write Rails 有attr_accessor_with_default所以你可以写

class Like
  attr_accessor_with_default :politics,false
end

i = Like.new
i.politics #=> false

and thats all就这样

UPDATE更新

attr_accessor_with_default has been deprecated in Rails 3.2.. you could do this instead with pure Ruby attr_accessor_with_default在 Rails 3.2 中已被弃用。你可以用纯 Ruby 来代替

class Like
  attr_writer :politics

  def politics
    @politics || false
  end
end

i = Like.new
i.politics #=> false

If you define your attr_accessor , by default the value is nil .如果您定义attr_accessor ,则默认值为nil You can write to it, thereby making it !nil你可以写它,从而使它成为!nil

You could use the virtus gem:您可以使用 virtus gem:

https://github.com/solnic/virtus https://github.com/solnic/virtus

From the README:从自述文件:

Virtus allows you to define attributes on classes, modules or class instances with optional information about types, reader/writer method visibility and coercion behavior. Virtus 允许您在类、模块或 class 实例上定义属性,并提供有关类型、读取器/写入器方法可见性和强制行为的可选信息。 It supports a lot of coercions and advanced mapping of embedded objects and collections.它支持嵌入对象和 collections 的大量强制和高级映射。

It specifically supports default values .它特别支持默认值

class Song < ActiveRecord::Base
    def initialize(*args)
    super(*args)
      return unless self.new_record?
      self.play_count ||= 0
    end
end

In my opinion, I think this answer is good.在我看来,我认为这个答案很好。 Although we are overriding the initialize() method but since super is called on the first line of this method, it doesn't change anything to the base class initialize() method, rather this is a key feature of Object Oriented programming that we can override it, and add some more functionality at the time of initialization of objects.虽然我们重写了initialize()方法,但由于在该方法的第一行调用了 super,它并没有改变基本的 class initialize()方法,而是 Object 的一个关键特性覆盖它,并在初始化对象时添加更多功能。 And also the initializer will not be skipped while reading objects from the database, since super is called on the first line of the function.在从数据库读取对象时也不会跳过初始化程序,因为在 function 的第一行调用了super

I've been using this form in ActiveRecord:我一直在 ActiveRecord 中使用这种形式:

class Song < ActiveRecord::Base

    def initialize(*args)
      super(*args)
      return unless self.new_record?
      self.play_count ||= 0
    end
end

This is not done in attr_accessor, rather, in your migration.这不是在 attr_accessor 中完成的,而是在您的迁移中完成的。

So.. you could perform a migration所以..你可以执行迁移

(1) rails g migration ChangePoliticsColumnToDefaultValueFalse (1) rails g migration ChangePoliticsColumnToDefaultValueFalse

(2) add this in def self.up (2) 在def self.up中添加这个

def self.up change_column:your_table, :politics, :boolean, :default => 0 end

The bottom line is that default attributes are set in migrations.底线是在迁移中设置了默认属性。

Hope this helps!希望这可以帮助!

Edit:编辑:

There are some similar questions too that have been answered well:还有一些类似的问题也得到了很好的回答:

Like here and here喜欢这里这里

attr_accessor in rails are only virtual variable which is not store in database and it can be use only until active record not save or update in application. rails 中的 attr_accessor 只是不存储在数据库中的虚拟变量,它只能在活动记录不保存或更新应用程序之前使用。 You can define attr_accessor in model like您可以在 model 中定义 attr_accessor

class Contact < ActiveRecord::Base
attr_accessor :virtual_variable
end

and use in form like:-并以如下形式使用:-

 <div class="field">
    <%= f.label :virtual %><br>
    <%= f.text_field :virtual %>
  </div>

and you can found these vartual variable value in controller param...您可以在 controller 参数中找到这些虚拟变量值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM