views:

101

answers:

1

I have a database of sports teams that I am displaying in tables/standings. The relevant code involves two models, Tableposition and Draw, which are associated in a has_one relationship. The following static named scope declaration works perfectly:

class Tableposition < ActiveRecord::Base
  belongs_to :draw
  named_scope :grouptable, :include => :draw, :conditions => ['draws.group = ?', "B"]
end

However, when I try to make it dynamic:

class Tableposition < ActiveRecord::Base
  belongs_to :draw
  named_scope :grouptable, :include => :draw, 
                lambda { |group| { :conditions => ['draws.group = ?', group] } }
end

I get the following error:

SyntaxError: /.../app/models/tableposition.rb:4: syntax error, unexpected '\n', expecting tASSOC

I've scoured the web for solutions and have tried converting the curly braces to a do ... end with brackets to no avail. Any thoughts would be hugely appreciated.

+4  A: 

named_scope wants arguments like this: (name, options) You were giving it (name, include option, condition option) Where both include and condition options were hashes. Instead you need to give it one merged hash.

Corrected code:

named_scope :grouptable, lambda { |group|
  {  :include => :draw, :conditions => ['draws.group = ?', group] } 
}
EmFi
You are a god. Thanks!!!
Thanks. Around here that's shown by voting up and accepting an answer. A green checkmark goes a long way in helping others who have a similar questions.
EmFi
Will do. Don't have enough "rep" to do it yet, but I'll come back when I do. Thanks again.