adding extra variable to a class, how to access it?

Jan Eden schrieb:

Ross Riley wrote:

or attr_accessor :variable

Ross

Strangely enough, this does not work for me. I tried:

class Page < ActiveRecord::Base   belongs_to :author   attr_accessor :typus end

and was not able to access somepage.typus in my view. However, this worked:

class Page < ActiveRecord::Base   belongs_to :author   def typus=(typus_value)         write_attribute(:typus, typus_value)   end

  def typus     read_attribute(:typus)   end end

Why is it that attr_accessor does not work for an AR-based class?

Thanks, Jan

Hi Jan,

did you try to access a persistent or a non-persistent attribute?

What do you get when you @page.typus in your view, I guess nil unless you've done something like @page.typus = 'something' before...

Regards Florian

Hi Jan

    @children.each do |child|       child.author = nil if child.author == page.author       child.typus = (child.visible_children != nil) ? 'Node' : 'Page'     end

In this case I'd drop

  child.typus = (child.visible_children != nil) ? 'Node' : 'Page'

and def typus in the model itself:

  def typus     visible_children ? 'Node' : 'Page'   end

What's your code in the view? I can't understand why accessor shouldn't work...

Regards Florian

I'm very curious about this one. For me, declaring accessors, has always worked in my whole rails code...

The objects in children in your view are really instances of Page? Did you make a child.inspect in vour view or child.methods.sort or maybe even child.instance_variable_get(:@typus)?

I use inspect and methods(+grep) very often at frontend debugging...

Regards Florian