mysql - What kind a SQL query use rails to retrieve data in console -


when fire user.first.email in rails - 4 console gives me query select "users".* "users" order "users"."id" asc limit 1 query structure how can email column database firing select "users".* "users" in rails. please provide needful description this.

if want query equivalent select "users".* "users" should using following in console:

user.all 

if want select email field, generating query select "users"."email" "users" use pluck modifier, return array of emails:

user.pluck(:email) 

notice we've dropped all query modifier. isn't required in instance, can add clarity if like:

user.all.pluck(:email) 

be aware though return array containing requested attribute, not instances of model belong to.

for that, use select modifier:

user.select(:email) #or user.all.select(:email) 

this return instances of user class email populated data db. useful if need chain query others, less performant pluck alternative large dataset.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -