Keep getting syntax error

Hey all,

I keep getting a syntax error somewhere in this code. Thanks for all help.

class StudentState < ActiveRecord::Base has_many :students end

class Student < ActiveRecord::Base belongs_to :student_state named_scope :filtertabs, :join => :student_state, lambda { |*args| {:conditions => {'student_state.id => ?', args}} } end

class StudentsController < ApplicationController   before_filter :get_status

  def index   @list = student.find(:all)   render :xml => @list    end

def filter      @students = @student_state.students.filtertabs.find(:all)     render :xml => @students end

def get_status       @student_state = StudentState.find(params[:id]) end

This is wrong - I assume you meant 'student_state.id' => args

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

After I change it to:

named_scope :filtertabs, :join => :student_state, lambda { |*args| {:conditions => {'student_state.id' => args}} }

I still get a syntax error.

Your lambda should return a single hash with all the query options you want. (and its :joins not :join)

Fred

Frederick Cheung wrote:

Frederick Cheung wrote: >> Frederick Cheung wrote:

>> After I change it to:

>> named_scope :filtertabs, :join => :student_state, lambda { |*args| >> {:conditions => {'student_state.id' => args}} }

>> I still get a syntax error.

> Your lambda should return a single hash with all the query options you > want. (and its :joins not :join)

> Fred

Isn't it returning a single hash here?

No it isn't - you've got the :join => :student_state and then you've got the lambda - you can't mix and match the proc form of named_scope with the non proc form: either you pass a hash to named_scope or you give it a lambda - not both.

Fred

Frederick Cheung wrote:

> Your lambda should return a single hash with all the query options you > want. (and its :joins not :join)

> Fred

Isn't it returning a single hash here?

No it isn't - you've got the :join => :student_state and then you've got the lambda - you can't mix and match the proc form of named_scope with the non proc form: either you pass a hash to named_scope or you give it a lambda - not both.

Fred

Well, I changed it around. I think this is what you were talking about:

class StudentState < ActiveRecord::Base has_many :students end

class Student < ActiveRecord::Base belongs_to :student_state named_scope :status, :joins => :student_state, => {'student_state.state' => ?, :state} end

class StudentsController < ApplicationController

before_filter :get_state

def index @list = student.find(:all) render :xml => @list end

def filter_show @student_state.student.status(:state) #This is the students associated with a student state and based on which value they click on, we will return that value into the status named_scope, which will set it and then only return the students of the student_status which has a value of that specific state (e.g. passing/failing) end

def get_state @student_state= StudentState.find(params[:all]) #All of the attributes of student_state model (e.g. status attribute and values of passing/failing) end

All I was trying to do was where a user clicks on one of options in a filter, such as passing, then it will return the list of students who are passing. My undertanding is this could be done in named_scope.

John Merlino wrote:

Frederick Cheung wrote:

> Your lambda should return a single hash with all the query options you > want. (and its :joins not :join)

> Fred

Isn't it returning a single hash here?

No it isn't - you've got the :join => :student_state and then you've got the lambda - you can't mix and match the proc form of named_scope with the non proc form: either you pass a hash to named_scope or you give it a lambda - not both.

Fred

Well, I changed it around. I think this is what you were talking about:

class StudentState < ActiveRecord::Base has_many :students end

class Student < ActiveRecord::Base belongs_to :student_state named_scope :status, :joins => :student_state, => {'student_state.state' => ?, :state} end

class StudentsController < ApplicationController

before_filter :get_state

def index @list = student.find(:all) render :xml => @list end

def filter_show @student_state.student.status(:state) #This is the students associated with a student state and based on which value they click on, we will return that value into the status named_scope, which will set it and then only return the students of the student_status which has a value of that specific state (e.g. passing/failing) end

def get_state @student_state= StudentState.find(params[:all]) #All of the attributes of student_state model (e.g. status attribute and values of passing/failing) end

All I was trying to do was where a user clicks on one of options in a filter, such as passing, then it will return the list of students who are passing. My undertanding is this could be done in named_scope.

Actually, ignore above. This to me makes sense as to how it should work:

class StudentState < ActiveRecord::Base has_many :students end

class Student < ActiveRecord::Base belongs_to :student_state named_scope :status, :joins => :student_state, => {'student_state.state' => ? = student_state.students.state_id, :state} end

class StudentsController < ApplicationController

before_filter :get_state

def index @list = student.find(:all) render :xml => @list end

def filter_show

@filter = @student_state.students.status(:state) #This is the students associated with a student state and based on which value they click on, we will return that value into the status named_scope, which will set it and then only return the students of the student_status which has a value of that specific state (e.g. passing/failing) end render :xml => @filter end def get_state @student_state= StudentState.find(:all) #All of the attributes of student_state model (e.g. status attribute and values of passing/failing) end

Not quite - since you've got a parameter you have to use the lambda form (when I said you had to use the hash form or the lambda form I was talking in generalities), so you need

named_scope :status, lambda {|...| { :conditions => ..., :joins => ... }

Fred

Frederick Cheung wrote:

=> ?, :state} end

Not quite - since you've got a parameter you have to use the lambda form (when I said you had to use the hash form or the lambda form I was talking in generalities), so you need

named_scope :status, lambda {|...| { :conditions => ..., :joins => ... }

Fred

named_scope :status, lambda { |key|   { :joins => :student_state, :conditions => ["student_state.key = ?", key], :limit => 20 } }

The above ended up working. However, I thought that the inner join I specified would make the attributes of student_state available in the xml output of the students controller (when going to the url http://localhost:3000/students.xml). But it wasn't. Is this a limitation of rail's xml capabilities?

The :joins / :includes specified when you fetch rows have no bearing on xml (or json etc) output, however to_xml does accept options that allow you to do that sort of stuff.

Fred