Ruby rails - select only few columns from the data base
Rails 3:
Item.select("name, address").where( .... )
How to find a record by id and select few columns, ruby on rails?
You can try this. I hope this will help.
@user = User.where("id = ?", 4).select( "user_fname, user_lname")
How to select only specific columns when querying using ActiveRecord?
In your post model you can define the association as follows:
belongs_to :user, :select => [:username]
Then referring to the user via a post would select only the stated columns.
ActiveRecord find and only return selected columns
In Rails 2
l = Location.find(:id => id, :select => "name, website, city", :limit => 1)
...or...
l = Location.find_by_sql(:conditions => ["SELECT name, website, city FROM locations WHERE id = ? LIMIT 1", id])
This reference doc gives you the entire list of options you can use with .find
, including how to limit by number, id, or any other arbitrary column/constraint.
In Rails 3 w/ActiveRecord Query Interface
l = Location.where(["id = ?", id]).select("name, website, city").first
Ref: Active Record Query Interface
You can also swap the order of these chained calls, doing .select(...).where(...).first
- all these calls do is construct the SQL query and then send it off.
In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array)
Since there is guaranteed to be only one row, you can grab it directly via .first
or .last
. While you may wish to limit the selected column only to the desired breakfast
with the .select()
method, it probably isn't necessary and you can just retrieve breakfast
from the resultant object:
Plan.last.breakfast
=> 100
# Generates this query:
Plan Load (0.4ms) SELECT "plans".* FROM "plans" ORDER BY "plans"."id" DESC LIMIT 1
Using .select()
to cause the SQL query to retrieve only the breakfast
column is possible, but won't give you that much benefit other than a very tiny memory increase.
Plan.select(:breakfast).last.breakfast
=> 100
# Generates this query:
Plan Load (0.4ms) SELECT breakfast FROM "plans" ORDER BY "plans"."id" DESC LIMIT 1
Of course, you can store it to a variable, or use it right in your expression:
if Plan.last.breakfast > 90
# whatever
end
Retrieving only certain columns with Rails 3 where clause
results = Model.where(:lat => (south..north), :lng => (east..west)).select([:lat, :long, :id])
You'll also probably want to include the :id
in that select if you want your results to behave like reasonable ActiveRecord
objects.
Edit: Select takes a single arg, can be an array.
in Rails how to select additional columns from multiple tables
result = User.first.usertags.joins(:tag).select("tags.name as tag_name, tags.id, usertags.id, user.id")
result.collect{|r| r.tag_name}
Related Topics
Rails Search with Optional Parameters
How to Avoid Putting the Magic Encoding Comment on Top of Every Utf-8 File in Ruby 1.9
Ruby/Rails - .Each Iterator Is Printing Entire Array at the End of the Loop
Rails Migration Changing Column to Use Postgres Arrays
How to Switch to an Older Version of Rails
How to Install Ruby-Debug in Ruby 1.9.3/Rails 3.2.1
Writing Over Previously Output Lines in the Command Prompt with Ruby
Repairing Postgresql After Upgrade to Osx Mavericks
Ruby - Determine If a Number Is a Prime
How to Set the Default String Encoding on Ruby 1.9
What Does the Fail Keyword Do in Ruby
What Does "Wrong Number of Arguments (1 for 0)" Mean in Ruby
How to Get the Latest Record from Each Group in Activerecord
Is There a Literal Notation for an Array of Symbols
How to Write a Ruby Command Line App That Supports Tab Completion