Forum: Ruby on Rails HABTM & find_all_by

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.
Kenny W. (Guest)
on 2009-05-14 05:30
Hello!

I'm a little new to rails and have a strange problem i'm trying to
decipher.

I have a Project model and Category model, connected together by a HABTM
(with a categorys_projects join table)

Rails is doing everything as it should, apart from the find.

Am I wrong to think that I should be able to go:-

projects = Project.find_all_by_category(some_id)

As the only way I can return projects of a certain category right now is
using
a join like so :

projects = Project.find(:all, joins => :categories, :conditions =>
{:categories => { :id => params[:category] } })

Where:-

class Project < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

Bit confused!

Any help would be great

Thanks!
K
Bob (Guest)
on 2009-05-14 11:41
(Received via mailing list)
Hi

how about this?

category = Category.find some_id
projects = category.projects

projects = Project.find_all_by_category(some_id)  will not work,
because find_all_by_... methods work only for existing columns in
table

Hope it helps )
Kenny W. (Guest)
on 2009-05-14 14:24
Bob wrote:
> Hi
>
> how about this?
>
> category = Category.find some_id
> projects = category.projects
>
> projects = Project.find_all_by_category(some_id)  will not work,
> because find_all_by_... methods work only for existing columns in
> table
>
> Hope it helps )

I think I understand what you're saying!

Thanks a bunch!
This topic is locked and can not be replied to.