2010-07-06 36 views
2

我有兩個型號看起來像這樣由協會(DataMapper的)

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 

我試圖找到具有分配一個特定的職員各個階段查找。我試過@stages = Stage.all(Stage.Staff => 'TM')

我在做什麼錯?

回答

1

試試這個,它已經有一段時間,因爲我用的DataMapper。

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

這是假設「TM」實際上是您用於工作人員ID的值。

+0

謝謝,這很有幫助。 – Tom 2010-07-10 09:35:55

0

我做

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

它不返回錯誤,但不返回任何數據。如果我在其他領域如標題上運行它,它可以正常工作,但不適用於外鍵? – Tom 2010-07-06 08:20:25

+0

@Tom我無法測試它,我建議嘗試使用以下鏈接指示關聯:鏈接例如'Stage.all(:links => [:staff],Stage.staff.id =>'TM')' – philant 2010-07-09 19:42:00

2

其實你可以使用字符串鍵在DataMapper的是這樣的:

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

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

你可以混合和匹配與該型號性能以及:

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

這將得到屬於名字以'Ted'開頭並且id大於或等於5的人的所有階段。

+0

不知道%,謝謝 – Tom 2011-01-22 20:19:19

+0

要清楚,%是SQL部分字符串匹配語法。您也可以完成Stage.all('staff.name'=>/^ Ted /):)並且DataMapper足夠聰明,可以知道如何進行匹配/翻譯。 – knowtheory 2011-01-23 19:22:19