在我的應用程序,puts self.ideas.inspect
輸出:第一次嘗試使用ActiveRecord「在哪裏」,沒有
[#<Idea id: 1, title: "Ad et ut nesciunt ut.",
note: ["Ratione et aut harum.", "Dicta nostrum sit officiis voluptates molestiae veritatis ut."],
status: "claimed",
created_at: "2012-12-27 19:03:11", updated_at: "2012-12-27 19:03:11", user_id: 1>,
#<Idea id: 2, title: "Asperiores dolores modi et.",
note: ["Reprehenderit harum consequuntur repellendus qui.", "Labore eum minus voluptas quidem aliquid est occaecati."],
status: "claimed",
created_at: "2012-12-27 19:03:11", updated_at: "2012-12-27 19:03:11", user_id: 1>,
#<Idea id: 3, title: "Labore nam deserunt quibusdam.",
note: ["Sequi saepe itaque.", "Numquam distinctio vel hic et qui."],
status: "claimed",
created_at: "2012-12-27 19:03:11", updated_at: "2012-12-27 19:03:11", user_id: 1>]
然而,在下一行,claimed_count = self.ideas.where(status: "claimed").size
收益0.我的查詢語法顯然是錯誤的,但我一直看看this guide,我仍然無法弄清楚!
必須是非常基本的東西...任何人?
編輯:我得到這個集合使用下面的測試思路:
it "can't claim an idea if he already has 3 ideas with claimed status" do
james.claim(si_title0)
james.claim(si_title1)
james.claim(si_title2)
james.claim(si_title3)
james.ideas.size.should eq 3
end
我的醜測試對不起...我要去揣摩插值後重構它我得到它通過。上面的inspect
轉儲來自循環的第三次或第四次迭代。
編輯2:這是claim
做:
所以......它不保存到數據庫?如果是這樣,我該如何解決?
你是如何得到這個「想法」的集合的?當您呈現它時,它是否已經保存到數據庫中? – PinnyM
'.count'是不是'.size'? – Zabba
@PinnyM - 編輯添加上面的調用代碼。 – ezuk