order by associated model position

I have a set of questions specific to a user, now I want to order them by a position given in an associated model.

The associated model is called questions_postions and have a position integer column. Now the questions may be there, or it may be nil.

I've tried:

 employee.questions.order(questions.positions.position or 0).each do |q|

but that returns:

undefined local variable or method `questions_positions'

the associations are setup:

Question:

has_many :questions_positions

QuestionsPosition:

class QuestionsPosition < ActiveRecord::Base
  belongs_to :question
  belongs_to :ownerable, polymorphic: true
end

Upvotes: 1

Views: 108

Answers (1)

Yury Lebedev
Yury Lebedev

Reputation: 4015

employee.questions.joins(:positions).order('positions.position DESC')

Upvotes: 1

Related Questions