Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 639 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ActiveRecord select except columns

#1
Is there a way I can specify to select ALL columns in ActiveRecord except just a few. For example, for a User, I don't want to select their password hash or their email. Is this possible or do I have to manually hardcode all columns?

Thanks
Reply

#2
Something like this?

exclude_columns = ['password', 'email']
columns = User.attribute_names.delete_if { |x| exclude_columns.include?(x) }

User.select(columns)

**EDIT**

I forgot that we can do **Array1 - Array2**

A best answer:

exclude_columns = ['password', 'email']
columns = User.attribute_names - exclude_columns

User.select(columns)

Reply

#3
Just to add an option for people who want to exclude certain columns in every query by default can use the Rails 5 feature `ignored_columns`, which as the name suggests leaves those out.

The code is quite straightforward:

class User < ApplicationRecord
self.ignored_columns = %w[password email]
end

Please note that the [ignore_columns documentation][1] states that `You will get an error if accessing that attribute directly, so ensure all usages of the column are removed` so it may only work for cases where the columns are never required to be used.

[1]:

[To see links please register here]

Reply

#4
Another really usefull way it's with a scope inside the model in case you need to avoid a column constantly.

In my case I save images into blob fields so I want to avoid those to be loaded everytime and in a easy way:

scope :select_exclude_image, -> { select( Movie.attribute_names - ['image'] ) }

Then to avoid the image in the selection you can do something like that:

Movie.select_exclude_image.first
or

Movie.select_exclude_image.all

I hope it will help!
Reply

#5
In some cases, particularly when you're considering a `default_scope` to exclude certain columns, I would advise against this approach because of a [known issue][1] with Rails about `count` breaking after a `select` clause. This can lead to surprising downstream errors.

In such a case, consider instead breaking your record into two tables, one with the essential data, the other one with the data you only need from time-to-time, then use associations to access the extra data.


[1]:

[To see links please register here]

Reply

#6
write a scope like

def select_without columns
select(column_names - columns.map(&:to_s))
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through