Sorting with find(:all, :order) and belongs_to

I want to sort a collection of tasks based on their project name, but
the project name is referenced thru project_id in the task table and
belongs_to in the task-model.

I can’t seem to find the proper way to use find(:all, :order => ?)
correctly. How do I sort based on a joined table? Thanks!

you can do something like order=“desc”

Use the :include parameter of find to load the associated projects
to allow you to sort

eg Task.find(:all, :include => :project, :order => :project_name)

eg Task.find(:all, :include => :project, :order => :project_name)

It doesn’t work I’m afraid. At least not when using it in my context,
which is:

<% timeframe.tasks.find(:all, :order => ‘project_id’).each do |@task| %>

<% timeframe.tasks.find(:all, :include => :project, :order =>
‘projects.name’).each do |task| %>

It worked, thanks a million! However, I’m keeping the instance variable
'cause it’s the only way I can seem to get my checkboxed to show up as
checked:

<%= check_box ‘task’, ‘completed’, :onchange => remote_function( :url =>
{ :action => ‘toggle_completed’, :id => @task }, :with =>
“‘id=#{@task.id}’” ) %>

Any suggestions on why it doesn’t work with a local variable is much
appreciated :wink:

Thanks again!

On Oct 10, 2007, at 9:42 AM, Gustav S. wrote:

eg Task.find(:all, :include => :project, :order => :project_name)

It doesn’t work I’m afraid. At least not when using it in my context,
which is:

<% timeframe.tasks.find(:all, :order => ‘project_id’).each do |
@task| %>

<% timeframe.tasks.find(:all, :include => :project, :order =>
‘projects.name’).each do |task| %>

Note that I’ve changed your block variable to a local rather than an
instance variable. When you eager-load an associated model (:include
=> :project), you need to make sure that the sql fragment in the
order option is valid which often means prepending the table name
(particularly if the column name appears in more than one of the
tables involved… or could in the future).

-Rob

Rob B. http://agileconsultingllc.com
removed_email_[email protected]

On 10 Oct 2007, at 16:22, Gustav S. wrote:

( :url =>
{ :action => ‘toggle_completed’, :id => @task }, :with =>
“‘id=#{@task.id}’” ) %>

Any suggestions on why it doesn’t work with a local variable is much
appreciated :wink:

check_box and its instance variable convention. Use form/fields_for
or use check_box_tag

Fred

Rob B. wrote:

… which you can overcome by giving an :object option:

<%= check_box(‘task’, ‘completed’, :object => task,
:onchange => remote_function(
:url => { :action => ‘toggle_completed’, :id => task },
:with => “‘id=#{task.id}’” )) %>

It worked, thanks!

Do you really need that :with option if the :url has the :id already?

I can’t seem to get the passing of the ID to the RJS-template to work
otherwise. My RJS-template looks like this:

page[“task_#{params[:id]}”].toggle_class_name “completed”

Is there another way? Thanks again!

On Oct 10, 2007, at 11:32 AM, Frederick C. wrote:

<%= check_box ‘task’, ‘completed’, :onchange => remote_function
Fred
… which you can overcome by giving an :object option:

<%= check_box(‘task’, ‘completed’, :object => task,
:onchange => remote_function(
:url => { :action => ‘toggle_completed’, :id => task },
:with => “‘id=#{task.id}’” )) %>

Do you really need that :with option if the :url has the :id already?

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Oct 11, 2007, at 5:28 AM, Gustav S. wrote:

Do you really need that :with option if the :url has the :id already?

I can’t seem to get the passing of the ID to the RJS-template to work
otherwise. My RJS-template looks like this:

page[“task_#{params[:id]}”].toggle_class_name “completed”

Is there another way? Thanks again!

Does the url get generated with
:url => { :action => ‘toggle_completed’, :id => task },

put the task id in somewhere? If your routes are set up, then I’d
expect that you’d only be redundant with the explicit ?id= from
the :with.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs