ruby on rails - How to eagerly load unassociated active model records -
i have 2 models: hurricane
, problem
, each hurricane
has many problem
now in database want let users update data each hurricane, have 1 copy of data set admin , each user has access data. when each user wants update data hurricane, create new copy of admin created record , scope each user sees newly created record modified. , each new record stores , 'original_id' attribute stores id of model cloned from. however, each problem model still point original hurricane record (created admin). should mention each problem belongs_to
user
for given user, when listing problems, i'd eagerly load associated hurricanes, , instead of displaying hurricanes point to, i'd eagerly load ones modified
i tried this:
in app/models/problem.rb
class problem < activerecord::base belongs_to :hurricane belongs_to :user_hurricane, -> (problem) { "original_id = ? , user_id = ?", problem.hurricane_id, problem.user_id }, class_name: 'hurricane' ...
then call think following
problems = problem.includes(:user_hurricanes).where(user_id: current_user.id) problems.each |problem| puts problem.user_hurricane end # >> returns nil
but user_hurricane nil, doesn't eagerly load user specific hurricane records. how that?
Comments
Post a Comment