delagoya (owner)

Revisions

gist: 32469 Download_button fork
public
Description:
Example DM polymorphic association
Public Clone URL: git://gist.github.com/32469.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# the notes model
 
class Note
  include DataMapper::Resource
  property :id, Serial
  property :noteable_class, String , :nullable => false, :index => true
  property :noteable_id, Integer, :nullable => false, :index => true
  property :text, Text, :nullable => false
 
  def noteable
    begin
      eval( "#{noteable_class}.get!(#{noteable_id})") if noteable_class and noteable_id
    rescue
      return nil
    end
  end
end
 
# in the parent model
 
  has n, :notes, :child_key => [:noteable_id], :noteable_class => self.class
 
  def add_note(n)
    notes << Note.new(:text=>n, :noteable_class => self.class)
  end