Reputation: 661
I am new to using DataMapper and simply want to add a property from one class, to a table in another. I have two classes, as seen below: I would like to add the 'handle' property from the class 'user', as a column in the table for 'peep'. I have required all the relevant gems (not included below) but am struggling with the DataMapper syntax. I have tried variants of has n, :user
and belongs to
etc, but all result in a 'user_id has NULL values'
errors.
Class 'user':
class User
include DataMapper::Resource
property :id, Serial
property :email, String, format: :email_address, required: true, unique: true
property :handle, String, required: true
end
Class 'peep':
class Peep
include DataMapper::Resource
property :id, Serial
property :peep_content, String
property :created_at, DateTime
end
Upvotes: 0
Views: 77
Reputation: 769
Seems duplicated with your another question:Data Mapper Associations - what code?
But here is my answer, when the association keys are not conventional id
or ..._id
, you have to specify it explicitly when you add the association, to let DataMapper know how to query the relation for you.
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
Upvotes: 0