Im reading the Rails guides for Rails 3 and they use this method:
cattr_accessor :attribute
What is this method? Is it a Rails method? I've never seen it before.
Im reading the Rails guides for Rails 3 and they use this method:
cattr_accessor :attribute
What is this method? Is it a Rails method? I've never seen it before.
It is a rails thing. Basically like the attr_* methods, but for the class level. One thing you wouldn't expect is because it uses a backing @@ variable, the value shared between the class and all instances.
ree-1.8.7-2010.02 > class Foo
ree-1.8.7-2010.02 ?> cattr_accessor :bar
ree-1.8.7-2010.02 ?> end
=> [:bar]
ree-1.8.7-2010.02 > foo1 = Foo.new
=> #<Foo:0x4874d90>
ree-1.8.7-2010.02 > foo2 = Foo.new
=> #<Foo:0x4871d48>
ree-1.8.7-2010.02 > foo1.bar = 'set from instance'
=> "set from instance"
ree-1.8.7-2010.02 > foo2.bar
=> "set from instance"
ree-1.8.7-2010.02 > Foo.bar
=> "set from instance"