Hi. Im using Rails 3.0.9 and Activescaffold with JQuery.
As I read about this here:
http://groups.google.com/group/activescaffold/browse_thread/thread/d96173c2eee1a6?tvc=2&q=+search+second

And Here:   
https://github.com/activescaffold/active_scaffold/wiki/Search-on-second-order-association

As I  tried to do a second order field search, I will tell you my
problem:

I have 3 models: Document, Course and Material:

class Document < ActiveRecord::Base
  belongs_to :course
[...]

class Material < ActiveRecord::Base
  belongs_to :document
  delegate :course, :to => :document, :allow_nil => true
[...]

class Course < ActiveRecord::Base
  has_many :documents
[...]


As you can see , Material belongs_to Document belongs_to Course
All I wanted to do is to search a material for something like:
material.document.course.name (in a active record way)

I read about second order search on the links at the beginning of this
e-mail, and did the following:

Put the following line in app/models/material.rb
delegate :course, :to => :document, :allow_nil => true

And the following in app/controllers/materials_controller.rb
[...]
conf.field_search.columns = [:discipline, :document_due_date, :course]
conf.columns << :course
conf.list.columns.add :course
conf.columns[:course].search_sql = 'courses.name'
conf.columns[:document].includes = {:document => :course}
conf.columns[:course].search_ui = :string

The primary problem is that when I click in courses search field and
actually put some value in it, it returns me the following error in
log:

  SQL (0.6ms)  SELECT COUNT(DISTINCT `materials`.`id`) FROM
`materials` LEFT OUTER JOIN `disciplines` ON `disciplines`.`id` =
`materials`.`discipline_id` LEFT OUTER JOIN `documents` ON
`documents`.`id` = `materials`.`document_id` LEFT OUTER JOIN `users`
ON `users`.`id` = `materials`.`user_id` WHERE (((courses.name LIKE '%a
%')) AND (disciplines.id IN
(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,25)) AND
((`materials`.`document_id` = '76')))
Mysql2::Error: Unknown column 'courses.name' in 'where clause': SELECT
COUNT(DISTINCT `materials`.`id`) FROM `materials` LEFT OUTER JOIN
`disciplines` ON `disciplines`.`id` = `materials`.`discipline_id` LEFT
OUTER JOIN `documents` ON `documents`.`id` = `materials`.`document_id`
LEFT OUTER JOIN `users` ON `users`.`id` = `materials`.`user_id` WHERE
(((courses.name LIKE '%a%')) AND (disciplines.id IN
(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,25)) AND
((`materials`.`document_id` = '76')))
Completed 500 Internal Server Error in 446ms

ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column
'courses.name' in 'where clause': SELECT COUNT(DISTINCT
`materials`.`id`) FROM `materials` LEFT OUTER JOIN `disciplines` ON
`disciplines`.`id` = `materials`.`discipline_id` LEFT OUTER JOIN
`documents` ON `documents`.`id` = `materials`.`document_id` LEFT OUTER
JOIN `users` ON `users`.`id` = `materials`.`user_id` WHERE
(((courses.name LIKE '%a%')) AND (disciplines.id IN
(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,24,25)) AND
((`materials`.`document_id` = '76')))):


As you can see, the generated query calls for courses.name in a join
that doesnt yeld that table. Am I missing something obvious here?

At first, I thought that it could have something to do with
field_search, but I also tried with the normal search and had the same
issue.

Also this way, I have a problem in list. Although I have the following
line in app/model/course.rb

def to_label
   self.name
end

Instead of having the content of the 'name' field in list, it gives me
the instance id, something like: #<Course:0x00000003b0b640>
I know how to correct this with a virtual column in the material model
(something like self.document.course.name), but Im just saying, for
the sake of completion.

Any help will be greatly appreciated. Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en.

Reply via email to