sdawes sdawes - 5 months ago 10
Ruby Question

Data Mapper Associations - what code?

ive looked through the DataMapper docs but am struggling to see how to do this. I have two tables and I simply wish to add the 'handle' attribute from the User table, as a column to the Peeps table - as per below?

enter image description here

Answer

Doc: http://datamapper.org/docs/associations.html Customizing Associations section.

class User
  ...
  has n, :peeps, 'Peep',
    :parent_key => [ :handle ],      # local to this model (User)
    :child_key  => [ :user_handle ]  # in the remote model (Peep)
end

class Peep
  ...
  belongs_to :user, 'User',
    :parent_key => [ :handle ],      # in the remote model (Peep)
    :child_key  => [ :user_handle ]  # local to this model (User)
end