ActiveRecord select except columns
write a scope like
def select_without columns
select(column_names - columns.map(&:to_s))
end
Specify fields to exclude from Rails query result
You could use:
Client.select(Client.column_names - ["name", "some_other_column"])
Edit: Rails 5 also introduced ignored_columns
if you want to exclude columns by default.
https://github.com/rails/rails/pull/21720
https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-ignored_columns-3D
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.
Ignoring certain field when querying in Rails and ActiveRecord
Model#attribute_names
should return array of columns/attributes. You can exclude some of them and pass to pluck
or select
methods.
Something like this:
posts = Post.where(published: true).select(Post.attribute_names - ['enormous_field'])
Rails ActiveRecord query ForeignKey except column
You need to pass the except
option as part of the addresses
key:
Users.all.to_json(
include: {
addresses: {
except: :country
}
}
)
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.
Active record is not returning specific columns when using model.find
That's because you have serializer
or custom as_json
method for your model and you lack the field
How to mark a column for deletion or exclude in ActiveRecord PostgreSQL?
I've found the answer in Rails docs, there ignored_columns
that needs to be set: https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-ignored_columns-3D
Once the updated code is deployed, it should be safe to drop the column.
Related Topics
Cannot Load Such File -- Readline (Loaderror) When Running Heroku Create --Stack Cedar
Form Submitted Twice, Due to :Remote=>True
Multiple Sinatra Apps Using Rack-Mount
Rails: Validation in Model VS Migration
Sidekiq: Ensure All Jobs on the Queue Are Unique
Is There a Shorter Way to Require a File in the Same Directory in Ruby
Ruby on Rails: Debugging Rake Tasks
How to Use Redis in a Multi-Threaded Rails Environment? (Puma/Sidekiq)
Ruby: Proc.New { 'Waffles' } VS. Proc { 'Waffles' }
How to Install Nokogiri Ruby Gem with Mkmf.Log Saying Libiconv Not Found
How to Get a Date from a Week Number
Bundler Could Not Find Compatible Versions for Gem "Bundler":