DRY up model and save shared properties in a new model via association

6 views
Skip to first unread message

Philipp Kursawe

unread,
Jan 17, 2012, 5:17:33 PM1/17/12
to DataMapper
I have these 2 models:

class Slot
include DataMapper::Resource

belongs_to :license

property :id, Serial
property :slot_nr, Integer
property :exp, DateTime
property :user_count, Integer
end

class SlotInfo
include DataMapper::Resource

property :id, Serial
property :name, String, :required => true
property :visible, Boolean, :default => lambda { |r, p|
default_visible(r, p) }
property :category, String

def name
SlotInfo.get(slot_nr).name
end

def visible?
SlotInfo.get(slot_nr).visible?
end
end

Is there a way to "merge" the name and visible properties from
SlotInfo into Slot via association?
So that I could omit the name and visible? methods and get these
values transparently through a join query when fetching a Slot from
the db?

Something like:
property :visible, :through => :slot_info, :key => :slot_nr

It works with the current code, but I cannot fetch all visible slots
via the license association at the moment:
"license.slots.visible" would be nice to have.
Reply all
Reply to author
Forward
0 new messages