Forum: Ruby on Rails Rails Edge, has_many :through in searches

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D9d1b31a337f46ac5e4dd56b55404f58?d=identicon&s=25 Dylan Markow (Guest)
on 2006-02-11 16:51
(Received via mailing list)
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?
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-11 17:03
(Received via mailing list)
On 2/11/06, Dylan Markow <dylan@dylanmarkow.com> 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 Olson
http://techno-weenie.net
D9d1b31a337f46ac5e4dd56b55404f58?d=identicon&s=25 Dylan Markow (Guest)
on 2006-02-11 17:08
(Received via mailing list)
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.
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-11 17:15
(Received via mailing list)
I believe this changeset fixes that problem.

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


Bob Silva
http://www.railtie.net/
D9d1b31a337f46ac5e4dd56b55404f58?d=identicon&s=25 Dylan Markow (Guest)
on 2006-02-11 17:16
(Received via mailing list)
That's what I thought, but my project is frozen on 3570, which should
include that.
D9d1b31a337f46ac5e4dd56b55404f58?d=identicon&s=25 Dylan Markow (Guest)
on 2006-02-23 00:12
(Received via mailing list)
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
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 Joshua Susser (Guest)
on 2006-02-23 01:48
Dylan Markow 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
This topic is locked and can not be replied to.