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
精彩评论