List of attributes from class and include module

ruby, ruby-on-rails

Solution

I can't promise that my description of the reason is correct but the following code should fix the problem. I believe that your are adding attributes to the Module not to the class that it is included within. Anyhow replacing your module with the following should fix the problem.

module BaseClueProperties

  def self.included(base)
    base.send :attr_accessor, :image_url
    base.send :attr_accessor, :summary_text
  end

end 

This should cause the including object to define attribute_accessors when it includes the module.

Problem

I have a class (ActorClue) that has three attr_accessor defined within it. There are a couple other common fields needed by other classes, so I put those common fields in a module (BaseClueProperties). I am including that module within my ActorClue class. Here's the code sampling : ``` module BaseClueProperties attr_accessor :image_url attr_accessor :summary_text end ################ class BaseClue # http://stackoverflow.com/questions/2487333/fastest-one-liner-way-to-list-attr-accessors in-ruby def self.attr_accessor(*vars) @attributes ||= [] @attributes.concat vars super(*vars) end def self.attributes @attributes end def attributes self.class.attributes end end ############### class ActorClue < BaseClue attr_accessor :actor_name attr_accessor :movie_name attr_accessor :directed_by include BaseClueProperties ..... end ``` I instantiate the above with the following : ``` >> gg = ActorClue.new => #<ActorClue:0x23bf784> >> gg.attributes => [:actor_name, :movie_name, :directed_by] ``` Why is it only returning :actor_name, :movie_name, and :directed_by and not include :image_url and :summary_text? I modified the BaseClueProperties to read the following : ``` module BaseClueProperties BaseClue.attr_accessor :image_url BaseClue.attr_accessor :summary_text end ``` But still with the same result. Any thoughts as to why my :image_url and :summary_text attributes aren't getting added to my @attributes collection?

Original source

Related problems