Rails ActiveRecord models can contain a lot of method calls and includes like this one I use:
class Food < ActiveRecord::Base
translates :name
belongs_to :group, class_name: 'FoodGroup', foreign_key: :food_group_id
has_many :components, through: :food_components
has_and_belongs_to_many :tags, join_table: 'tagged_foods', class_name: 'FoodTag'
scope :dishes, ->{where is_dish: true}
include PgSearch
pg_search_scope :whose_name_starts_with_en, :against => :name_en,
...
end
So my question is - when does all that code execute? Every time model instance is created? Or just once?
I have this question because I need to pass current_user.id
to pg_search_scope
, and not sure how safe it is to assume that every model will have a correct user id.