Given the following example taken from here:
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
I understand we’re creating a class based on the ActiveRecord::Base class. On the next line, what is happening with attr_accessible :content, :name, :title? Are we declaring a variable, creating a method, or calling one? What are the expressions like :some_variable? What are the colons for? Could someone explain in basic understanding what is happening here?
attr_accessibleis a method.Its argument is an array of symbols (the
:business, e.g.,:contentis a symbol).The method iterates over the list of properties-expressed-as-symbols and adds them to a list of properties it’s okay to mass-assign.
Most Ruby books should have discussed symbols.