author = Author.find_by_name(''bob'')
author.documents # => returns all the authors documents
if you have more conditions for the documents you can use normal .find
syntax here
author.documents.find( :all, :conditions => [''created_at >
?'', 2.weeks.ago])
On 6/26/05, Hugo Magalhaes
<hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello all.
> I have two tables: Document and Author. A Document has to have an Author.
> I want to search Documents that have a specific Author name. I know
> how to do the search by the id (foreign key), but I want also to do
> the search by the author''s name (another column in the authors
table).
> Can anyone tell me how to do this?
>
> Here is the code I use to search by author id:
>
> @documents = Document.find(:all, :conditions =>
["author_id=(?)", author_id])
>
> And the model classes:
>
> class Document < ActiveRecord::Base
> belongs_to :author
> end
>
> class Author < ActiveRecord::Base
> has_many :documents
> end
>
> Thanks for your answers,
> Hugo
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Tobi
http://www.snowdevil.ca - Snowboards that don''t suck
http://typo.leetsoft.com - Open source weblog engine
http://blog.leetsoft.com - Technical weblog