views:

10

answers:

1

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.

+1  A: 

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" 
Matt Briggs
Many thanks! I couldn't find that method in current API documentation so it has to be in Rails 3 only?
never_had_a_name
according to apidock.com, its been around since 2.1 :)
Matt Briggs