开发者

Rails where condition on un-persisted association

Is there any way开发者_开发技巧 to do

@destination.ratings.where(:name => 'monkey')

when none of the models are yet persisted?


No there isn't, but you may just use normal Ruby methods like Array#select to go through your unsaved models.

The where method and its bandmates in ActiveRecord generate SQL queries for the database, so if the model instances aren't in the database, it won't find anything.

Something like

@monkey = @destination.ratings.select{|rating| rating.name == 'monkey' }.first

could do the trick

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜