gustav
October 10, 2007, 11:48am
1
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!
gustav
October 10, 2007, 11:51am
2
you can do something like order=“desc”
gustav
October 10, 2007, 12:00pm
3
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)
gustav
October 10, 2007, 3:42pm
4
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| %>
gustav
October 10, 2007, 5:22pm
5
<% 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
Thanks again!
gustav
October 10, 2007, 4:09pm
6
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
[email protected]
gustav
October 10, 2007, 5:33pm
7
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
check_box and its instance variable convention. Use form/fields_for
or use check_box_tag
Fred
gustav
October 11, 2007, 11:28am
8
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!
gustav
October 10, 2007, 7:28pm
9
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]
gustav
October 11, 2007, 4:40pm
10
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]