If i understand correctly, you need to get data from this middle table
(PeopleName)?
attr = Person.get(YOUR_ID).people_names.first.link_type
So anyway you have to know what is your ID that refers to both
objects.
But i see the problem here, since you have many-to-many rels, you
might want to add additional field to your Person model to store
current name.
So it might look like this.
class Person
...
property :id, Serial
property :current_name, Integer, :required => true, :index => true
def current_name_attrs
self.names.get(self.current_name).person_names.first
end
end
And, take a look at my example:
----------------------------------------------------------------------------------
require 'rubygems'
require 'dm-core'
require 'dm-types'
DataMapper.setup(:default, 'sqlite3::memory:')
class Person
include DataMapper::Resource
property :id, Serial
property :title, String, :required => true
has n, :person_names
has n, :names, :through => :person_names
def pending_name
return self.person_names.first(:status => :pending).name
end
end
class Name
include DataMapper::Resource
property :id, Serial
property :title, String, :required => true
has n, :person_names
has n, :persons, :through => :person_names
end
class PersonName
include DataMapper::Resource
property :id, Serial
property :status, Enum[:active, :pending], :default => :active
belongs_to :person
belongs_to :name
end
DataMapper.auto_migrate!
# --------------------------------------------------------------------
p = Person.new(:title => "Person 1")
p.names << Name.new(:title => "Name 1")
p.names << Name.new(:title => "Name 2")
p.save
p = Person.new(:title => "Person 2")
p.names << Name.new(:title => "Name 3")
p.names << Name.new(:title => "Name 4", :person_names =>
[PersonName.new(:status => :pending, :person => p)])
p.save
people = Person.all
people.each do |p|
puts "#{
p.id} - #{p.title}"
p.names.each do |n|
puts "-> #{n.title}, status => #{n.person_names.first.status}"
end
end
puts Person.last.pending_name.inspect
OR, you can write your own SQL query to reach your point.
class Model
def custom_method
return adapter.query("SQL....")
end
end