activerecord - Ruby on Rails Unique Ordered Query -
what trying is:
find recent
competencylog (completed_at)
eachcompetency
oncourse
member
class member < activerecord::base has_many :competency_logs has_many :awards end class competencylog < activerecord::base belongs_to :member belongs_to :competency has_one :course, through: :competency end def course < activerecord::base has_many :competencies has_many :awards end class competency < activerecord::base belongs_to :course end
i have managed ordered list
course = course.find(params[:course_id]) current_member.competency_logs.where('competency_id in (?)', course.competency_ids).ordered
from here have tried few different things limited no success. recommendations appreciated. looking of in database possible speed since called , depended on ever changing timestamps on competencylog
the results want basically
member.competency_logs.where('competency_id in (?)', course.competency_ids).uniq.pluck(:competency_id)
but instead of competency_id want competency_log models
so i've added additional relationships , come following far, investigating postgresql distinct on
alternative
competency_logs = competencies.collect { |c| c.competency_logs.ordered.first } competency_logs.collect { |c| c.current? }.all?
the following work:
max_completed = current_member.competency_logs.where('competency_id in (?)', course.competency_ids).maximum(:completed_at) current_member.competency_logs.first(:conditions => {:completed_at => max_completed})
Comments
Post a Comment