sort_by on virtual column with parameter

10 views
Skip to first unread message

mpokress

unread,
Jan 9, 2012, 5:34:42 PM1/9/12
to actives...@googlegroups.com
Using AS 1.2 and Rails 2.3.14

I want to use the results of an instance method on the model (setup as a virtual column in AS) to sort. The instance method filters a collection of child objects based on parameters passed by the parent. Not sure if a proc would work here, and what the syntax would look like if it would. This controller code does not cause errors, but it also does not sort properly.

class Challenge < ActiveRecord::Base
  ...
  has_and_belongs_to_many   :users
  ...
end

class user < ActiveRecord::Base
  has_and_belongs_to_many   :challenges
  ...

  def training_sessions_by_challenge(challenge)
   #returns filtered collection of sessions that meet the criteria of the supplied challenge instance
  end

end

class UsersController < ApplicationController
before_filter :customize_config

  active_scaffold do |config|
    ...
  end

protected

  def customize_config
    if active_scaffold_constraints[:challenges]
      @challenge = Challenge.find(active_scaffold_constraints[:challenges].to_i
      active_scaffold_config.columns[:training_sessions_count].sort_by :method => "training_sessions_by_challenge(@challenge).count"
    end
end

Thanks in advance for any help,

Matt
Reply all
Reply to author
Forward
0 new messages