Re: Dynamic attribute-based finders

Mark Reginald James
2005-11-04T06:07:53Z

Mark Beattie wrote:

> Maybe there''s a better way to go about it. Basically, with 2
tables sharing a
> habtm relationship, I''d like to find all records in one table,
which have a
> relationship to a particular record in the other. The dynamic finders would
> seem to only be able to work on columns in a single table.

I think what you want is:

class A
   has_and_belongs_to_many :bs
end

a = A.find(id)
a.bs.find( :all, :conditions => ''...'' )

or, if :conditions are empty, just "a.bs".

The API docs state that a find on a habtm only takes an id
parameter, while the latest rails code clearly also supports
all non-dynamic forms of ActiveRecord::Base.find.

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000457

--
We develop, watch us RoR, in numbers too big to ignore.