# Rails Edge, has\_many :through in searches

**URL:** <https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917>\
**Category:** Rails\
**Created:** [February 11, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917 "2006-02-11T16:51:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 11, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/1 "2006-02-11T16:51:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 11, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/2 "2006-02-11T17:03:07Z")

</div>

On 2/11/06, Dylan M. [removed\_email\_address@domain.invalid](mailto: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](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 11, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/3 "2006-02-11T17:08:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 11, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/4 "2006-02-11T17:15:10Z")

</div>

I believe this changeset fixes that problem.

[http://dev.rubyonrails.org/changeset/3566](http://dev.rubyonrails.org/changeset/3566)

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 23, 2006, 12:12am UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/5 "2006-02-23T00:12:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 11, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/6 "2006-02-11T17:16:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dmarkow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bc8723/32.png) [@dmarkow](https://www.ruby-forum.com/u/dmarkow)\
**Post date:** [February 23, 2006, 1:48am UTC](https://www.ruby-forum.com/t/rails-edge-has-many-through-in-searches/65917/7 "2006-02-23T01:48:57Z")

</div>

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
