Rails where the condition is on transient association

Is there any way to do

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

when none of the models have survived?

+8
ruby-on-rails activerecord
source share
1 answer

No, no, but you can just use regular Ruby methods like Array#select to view unsaved models.

The where method and its groups in ActiveRecord generate SQL queries for the database, so if the model instances are not in the database, they won’t find anything.

Something like

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

could do the trick

+9
source share

All Articles