Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
dangerous.beans Avatar asked Sep 08 '11 18:09

dangerous.beans


1 Answers

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

like image 54
edgerunner Avatar answered Oct 04 '22 18:10

edgerunner