ruby on rails - Removing redundant queries using Active Record -
the following code works, runs far many sql statements liking;
user.includes([:profile,:roles]).select("id, email, created_at, confirmed_at, countries.name").find_in_batches |users| users.map{|u| # in here access profile.country.name} end
what meant grab user information along profile information , put csv format.
inside .map
calling profile.country.name
coming in fine rails running sql call find name, want capture information in initial lookup.
my issue because country
linked profile
, not user
can't add .includes
because doesn't see linkable table. there can force includes join table linked profile
?
the relevant model information below;
user:
has_one :profile, dependent: :destroy
profile:
belongs_to :user belongs_to :country
country:
has_many :profiles
you should able include country in original query nesting under profile in .includes call:
user.includes( {:profile => [:country], :roles} )
Comments
Post a Comment