← Back
activerecord rails

DB select exclude fields

Geographical areas stored in PostGIS are so big that we should not load them unless absolutely necessary, because performance.

I was doing this, that (as I wrote in my last highlight) it's a source of bugs:

# Don't do this
belongs_to :area, -> { select %i(id area_type name display_name metadata) }

Instead there is a very elegant solution:

scope :select_exclude_shape, ->  { select(Area.attribute_names - ['shape']) }

An then:

belongs_to :area, -> { select_exclude_shape }

Source: https://stackoverflow.com/questions/17431028/activerecord-select-except-columns

🖖