Rails Edge, has_many :through in searches


#1

I have two tables, a Projects table and a Clients table.

It’s basically a HABTM relationship, but I have additional project/
client-specific information in the join table. I’m trying to use the
new has_many :through method to join these. It works fine when
displaying records, but when I try to search, I’m having this problem:

When I used a HABTM model to search the project list for project and
client names, this worked fine:

@projects = Project.find(:all, :include => [:client], :conditions =>
[“client.name like ? or project.name like ?”, @query, @query])

However, doing this using the has_many :through technique doesn’t
seem to work, as it tries to joint “clients.project_id” to
“projects.id,” which won’t work because they’re not directly linked.
Is this a known issue, or is this by design?


#2

On 2/11/06, Dylan M. removed_email_address@domain.invalid wrote:

@projects = Project.find(:all, :include => [:client], :conditions =>
[“client.name like ? or project.name like ?”, @query, @query])

However, doing this using the has_many :through technique doesn’t
seem to work, as it tries to joint “clients.project_id” to
“projects.id,” which won’t work because they’re not directly linked.
Is this a known issue, or is this by design?

What do your model relationships look like? You can’t just take an
existing simple habtm and change it to has_many :through.


Rick O.
http://techno-weenie.net


#3

class Project < ActiveRecord::Base
has_many :clients_projects
has_many :clients, :through => :clients_projects
end

class Client < ActiveRecord::Base
has_many :clients_projects
has_many :projects, :through => :clients_projects
end

class ClientsProject < ActiveRecord::Base
belongs_to :client
belongs_to :project
end

Like I said, doing a @project.clients or @clients.project works fine
for displaying data, I just can’t get the :include => [:clients] to
work on finds.


#4

I believe this changeset fixes that problem.

http://dev.rubyonrails.org/changeset/3566

Bob S.
http://www.railtie.net/


#5

I just tried applying Patch 3816. The patch applied successfully, but
now I get Unknown column ‘clients_projects._id’ for my
Project.find(:all, :include => [:clients]). I would normally just do a
HABTM relationship, but I will be adding several fields to the join
table that I’d like to be able to access easily and update.

The SQL produced is:

LEFT OUTER JOIN clients_projects ON (clients_projects._id = projects.id
AND clients_projects._type = ‘Project’) LEFT OUTER JOIN clients ON
(clients.id = clients_projects.id AND clients_projects._id =
projects.id)

My clients_projects table only has an id, client_id, and project_id,
nothing else. Why is it looking for an “_id” and “_type” field?

My models:

class Project < ActiveRecord::Base
has_many :clients_projects
has_many :clients, :through => :clients_projects
end

class Client < ActiveRecord::Base
has_many :clients_projects
has_many :projects, :through => :clients_projects
end

class ClientsProject < ActiveRecord::Base
belongs_to :client
belongs_to :project
end


#6

That’s what I thought, but my project is frozen on 3570, which should
include that.


#7

Dylan M. wrote:

My clients_projects table only has an id, client_id, and project_id,
nothing else. Why is it looking for an “_id” and “_type” field?

Just as a guess, it sounds like something is looking for a polymorphic
association that is empty. Polymorphics use foo_id and foo_type fields
but in this case it looks like your foo is missing. I didn’t see any :as
options in your models though, so it’s probably a bug.

–josh