Sharing attributes between models

32 views
Skip to first unread message

Paul

unread,
May 30, 2013, 1:30:22 PM5/30/13
to datam...@googlegroups.com
Hi,

I have two models that share some of the same attributes so I wanted to setup a single table inheritance structure.  After doing some research, I found out that I can use module to achieve the same by doing the following instead:

module CommonFields
  def self.included base
    base.class_eval do
      include DataMapper::Resource

      property :type, base::Discriminator
      property :enable_feature1, base::Boolean, :default => false
      property :enable_feature2, base::Boolean, :default => false
    end
  end
end

class A
      include CommonFields

      property: title, String
end

class B
      include CommonFields
end

However, when I do rake db:automigrate I get:

undefined method `properties' for CommonFields:Module
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/associations/relationship.rb:252:in `parent_key'

Any ideas?

Thanks,

Paul

postmodern

unread,
May 30, 2013, 5:35:34 PM5/30/13
to datam...@googlegroups.com
I usually include DataMapper::Resource outside of the class/module eval.

base.send :include, DataMapper::Resource
https://github.com/ronin-ruby/ronin/blob/master/lib/ronin/model/model.rb
https://github.com/ronin-ruby/ronin/blob/master/lib/ronin/model/has_name.rb
--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datamapper+...@googlegroups.com.
To post to this group, send email to datam...@googlegroups.com.
Visit this group at http://groups.google.com/group/datamapper?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


-- 
Blog: http://postmodern.github.com/
GitHub: https://github.com/postmodern
Twitter: @postmodern_mod3
PGP: 0xB9515E77
signature.asc

Paul

unread,
May 30, 2013, 5:42:55 PM5/30/13
to datam...@googlegroups.com
Thanks for the info.  Unfortunately, I tried your suggestion but I still get the same error :(

undefined method 'properties' for CommonFields:Module

I am using datamapper 1.2.0 btw if it makes a difference.

Paul

unread,
May 31, 2013, 12:13:28 PM5/31/13
to datam...@googlegroups.com
I think I know what the problem is. What I didn't realize was that I had an association in class B that was referencing CommonFields. This is what's causing this problem! Need to rethink how I am going to do this.
Reply all
Reply to author
Forward
0 new messages