sql - ActiveRecord - Possible to pluck columns from a specific row? (Also, strange issues with select and quotation marks) -



sql - ActiveRecord - Possible to pluck columns from a specific row? (Also, strange issues with select and quotation marks) -

i trying number of columns specific row via activerecord (all examples simplified clarity):

user.pluck(:id, :name, :date_joined, :field_containing_json)

previously, using select statement reason didn't work unless each column enclosed in quotation marks:

user.find(id).select("id, name, date_joined, field_containing_json") fails reason , throws pg::syntaxerror: error while user.find(id).select("id", "name", "date_joined", "field_containing_json") works.

thus two-pronged question:

why 1 need encapsulate each column name in quotation marks when using select? is there way pluck specific row? user.find(id).pluck(:id, :name ...) throws nomethoderror

in first version, passing 1 argument, string "id, name, date_joined_field_containing_json". it's not sec version " encapsulating column names in quotation marks" -- it's sec version passing multiple arguments ruby arel select method, right way phone call it. multiple arguments, 1 each column. first 1 passing 1 argument select, single string includes spaces , columns. that's not select method wants.

yes, there way pluck specific row, so:

user.where(:id => id).pluck(:id, :name)

you can utilize whatever other more complex conditions in where might want also, , pluck columns.

i tested in rails 4.1; think pluck has been changing bit lately, it's possible doesn't work same in older rails.

sql ruby-on-rails postgresql activerecord sinatra

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -