Rails Polymorphic Association - Join elements with similar relationships -
i have system displays training resources skills , (basically) groups of skills on site. user can express interest in either skill or skill grouping stored in polymorphic table interests
.
i'd display training each skill or skill group interest on user home page, in view, want iterate through interests have training (not skills or skill groups have training).
both skill.rb
, skill_group.rb
models define training resources relationship.
_interests.html.erb
# iterate through interests <% @user.interests.each |interest| %> <% interest_element = interest.interest_element %> <% if interest_element.training_resources.size > 0 %> # more rendering code here... <% end %> <% end %>
interest.rb
class interest < activerecord::base belongs_to :user belongs_to :interest_element, :polymorphic => true end
ideally, i'd iterate through limited set of interests this:
@user.interests.joins(:interest_element => :training_resources).each |interest|
but can not eagerly load polymorphic association :interest_element
error
is there way can type of join?
try @user.interests.includes(:interest_elements) works in rails 4.
Comments
Post a Comment