Tom
Tom

Reputation: 34366

Find by association (Datamapper)

I've got two models that look like this

class Stage
    include DataMapper::Resource
    property :id, Serial
    belongs_to :staff
end

class Staff
  include DataMapper::Resource
  property :id, String, :key => true 
  property  :full_name, String 
  property  :email, String
  has n, :stages
end

I'm trying to find all Stages that have a specific Staff member assigned. I've tried @stages = Stage.all(Stage.Staff => 'TM')

What am I doing wrong?

Upvotes: 2

Views: 1078

Answers (3)

knowtheory
knowtheory

Reputation: 1055

Actually you can use string keys in datamapper like this:

Stage.all('staff.id' => 'TM')

or

Stage.all('staff.name.like' => 'Ted%')

You can mix and match with properties in that model as well:

Stage.all('staff.name.like' => 'Ted%', 'id.gte' => 5 )

That'll get all of the Stages belonging to people whose names start with 'Ted' and have an id greater than or equal to 5.

Upvotes: 2

Hendrik
Hendrik

Reputation: 2031

Try this, its been a while since i used DataMapper.

Stage.all(Stage.staff.id => 'TM')

This is assuming that the 'TM' would actually be the value you use for the id of the staff member.

Upvotes: 1

philant
philant

Reputation: 35816

I'd do

@stages = Stage.all(:staff => 'TM')

Upvotes: 0

Related Questions