ActiveRecord::Relation

Hi   I have just started learning Rails3 (ruby1.92, Rails3.0.5) and watched railscast episode 202. I tried Article.where(:hidden => true) from console. I have a confusion there since the cast says that , the above statement execute query in the view when iterated. I did not understand that. I got result from query in the console it self when executed Article.where(:hidden => true). Please comment

Thanks

Hi    i am talking exactly about this. Now only I found this

https://rails.lighthouseapp.com/projects/8994/tickets/4476-lazy-loading-broken-since-beta3

      I am getting this not only in console but application also. Has anything changed in Rails3.0.5?

Hi I have just started learning Rails3 (ruby1.92, Rails3.0.5) and watched railscast episode 202. I tried Article.where(:hidden => true) from console. I have a confusion there since the cast says that , the above statement execute query in the view when iterated. I did not understand that. I got result from query in the console it self when executed Article.where(:hidden => true). Please comment

The issue is that the console wants to print the result of the expression you type, which forces ActiveRecord to execute the query.

Fred

Hi Fred   Thanks . I tried from sample application also like   def index     @articles = Article.where(:hidden => true)     puts @articles.inspect

Hi Fred Thanks . I tried from sample application also like def index @articles = Article.where(:hidden => true) puts @articles.inspect ------------- end

  Here also the query executed\. I dont know why\.

because you called inspect on @articles

Fred

Hi    Then how can I check that it is not triggering sql. I did not understand it properly. Could you explain more please?

Frederick Cheung wrote in post #991438:

Hi Then how can I check that it is not triggering sql. I did not understand it properly. Could you explain more please?

Look in your development log and ensure that you don't touch the scope - when you do anything other than adding conditions, joins etc. to the scope you force active record to evaluate it.

Fred

the difference might be illustrated by the following:

Article.where(:hidden => true).to_sql

vs.

Article.where(:hidden => true).all.to_sql

in the first, you are able to get the (currently un-executed) sql statement

in the second, you will get an undefined_method error because having been evaluated, you now have an array (or a proxy to an array) instead of a scope.

does that help?