attr_accessor default values
Ruby on-RailsRubyAttr AccessorRuby on-Rails Problem Overview
I'm using rails and I want to make it so that attr_accessor :politics
is set, by default, to false.
Does anyone know how to do this and is able to explain it in simple terms for me?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
Rails has attr_accessor_with_default so you could write
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
class Like
attr_writer :politics
def politics
@politics || false
end
end
i = Like.new
i.politics #=> false
Solution 2 - Ruby on-Rails
If you define your attr_accessor
, by default the value is nil
. You can write to it, thereby making it !nil
Solution 3 - Ruby on-Rails
You could use the virtus gem:
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. It supports a lot of coercions and advanced mapping of embedded objects and collections.
It specifically supports default values.
Solution 4 - Ruby on-Rails
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. 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.
Solution 5 - Ruby on-Rails
I've been using this form in ActiveRecord:
class Song < ActiveRecord::Base
def initialize(*args)
super(*args)
return unless self.new_record?
self.play_count ||= 0
end
end
Solution 6 - Ruby on-Rails
This is not done in attr_accessor, rather, in your migration.
So .. you could perform a migration
(1) rails g migration ChangePoliticsColumnToDefaultValueFalse
(2) add this in 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:
Solution 7 - Ruby on-Rails
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. You can define attr_accessor in model like
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...