开发者

Rails where condition on un-persisted association

开发者 https://www.devze.com 2023-04-04 02:42 出处:网络
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 Ar

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

精彩评论

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