开发者

Find by association (Datamapper)

开发者 https://www.devze.com 2023-01-06 14:41 出处:网络
I\'ve got two models that look like this class Stage include DataMapper::Resource property :id, Serial belongs_to :staff

I've got two models that look like this

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

class Staff
  inclu开发者_如何学编程de 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?


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.


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.


I'd do

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号