# Keep getting syntax error

**URL:** <https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525>\
**Category:** rubyonrails-talk\
**Created:** [January 12, 2010, 2:25pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525 "2010-01-12T14:25:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 12, 2010, 2:25pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/1 "2010-01-12T14:25:45Z")

</div>

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 &nbsp;&nbsp;before\_filter :get\_status

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

def filter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@students = @student\_state.students.filtertabs.find(:all) &nbsp;&nbsp;&nbsp;&nbsp;render :xml =\> @students end

def get\_status &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@student\_state = StudentState.find(params[:id]) end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 12, 2010, 2:32pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/2 "2010-01-12T14:32:57Z")

</div>

This is wrong - I assume you meant 'student\_state.id' =\> args

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 12, 2010, 3:15pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/3 "2010-01-12T15:15:20Z")

</div>

Frederick Cheung wrote:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 12, 2010, 3:18pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/4 "2010-01-12T15:18:35Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 12, 2010, 4:58pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/5 "2010-01-12T16:58:26Z")

</div>

Frederick Cheung wrote:

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 12, 2010, 5:01pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/6 "2010-01-12T17:01:54Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 12, 2010, 6:33pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/7 "2010-01-12T18:33:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 12, 2010, 6:46pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/8 "2010-01-12T18:46:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 13, 2010, 11:11am UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/9 "2010-01-13T11:11:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 13, 2010, 2:06pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/10 "2010-01-13T14:06:40Z")

</div>

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| &nbsp;&nbsp;{ :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](http://localhost:3000/students.xml)). But it wasn't. Is this a limitation of rail's xml capabilities?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 13, 2010, 2:12pm UTC](https://discuss.rubyonrails.org/t/keep-getting-syntax-error/48525/11 "2010-01-13T14:12:49Z")

</div>

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
