ActiveRecord includes. Specify included columns
Rails doesn't have the facility to pass the options for include query. But we can pass these params with the association declaration under the model.
For your scenario, you need to create a new association with users model under the profile model, like below
belongs_to :user_only_fetch_email, :select => "users.id, users.email", :class_name => "User"
I just created one more association but it points to User model only. So your query will be,
Profile.includes(:user_only_fetch_email)
or
Profile.includes(:user_only_fetch_email).find(some_profile_ids)
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.
Rails 3 Joins -- Select only certain columns
You could use something like this:
@comments = Comment.joins(:user)
.select("comments.*, users.first_name")
.where(study_id: @study.id)
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.
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.
ActiveRecord select except columns
write a scope like
def select_without columns
select(column_names - columns.map(&:to_s))
end
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
How Does One Match Character or Nothing Using Regular Expression
Ruby JSON.Pretty_Generate ... Is Pretty Unpretty
Does Cucumber Do Away with The Need to Write Unit Tests
In Ruby, How to Remove Only 1 Match in an Array Easily
How to Redirect Back to a Page I'M Currently On
How to Get The Page Source with Mechanize/Nokogiri
Have a Parent Class's Method Access the Subclass's Constants
"No Available Formula for Gcc46" While Installing Ruby 1.9.3 on Os X with Rvm:
Compute Geo Distance in Elasticsearch
Access Slack Files from a Slack Bot
Using Negative Conditions Within Regular Expressions
Heroku and @Font-Face - Embedded Fonts Wont Display on Heroku
How to Associate a CSS/Sass Stylesheet to a View in Rails
What Is The Logic Behind This Result
How to Use Your Own MySQL Database Server with Heroku
Why Is Foreman Gem Ignoring the Port Environment Variable