Forum: Ruby on Rails Automatic finder

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.
Scott W. (Guest)
on 2006-04-13 06:46
(Received via mailing list)
It looks rails automatically creates a "finder" method on models that
have a "belongs_to".  So I tried this in the console:

c = Category.find(2)
chunks = Chunk.find_by_category(c)

chunks is always returned as an empty array.  When I know have some
chunks with a category_id of 2.

Should the find_by_category do what I think it should be doing or am I
completely off base?

thanks, cott.

----------------------------------------------------------------------------------------------------
What's an Intel chip doing in a Mac? A whole lor more that it's ever
done in a PC.

My Digital Life - http://scottwalter.com/blog
Pro:Blog - http://scottwalter.com/problog
Alex W. (Guest)
on 2006-04-13 09:25
(Received via mailing list)
First of all the find_by_xxx methods just return 1 record.  I think the
find_all_by_xxx is what you need.

Does this work?
chunks = Chunk.find_all_by_category_id(2)

I'm not entirely sure the dynamic finders work with associations, but
since "category_id" is a direct property of the model it shouldn't have
a problem with it.  Might make a nice patch though.

-Alex
Scott W. (Guest)
on 2006-04-13 16:23
(Received via mailing list)
yes that worked perfectly!  thanks.

----------------------------------------------------------------------------------------------------
What's an Intel chip doing in a Mac? A whole lor more that it's ever
done in a PC.

My Digital Life - http://scottwalter.com/blog
Pro:Blog - http://scottwalter.com/problog

----- Original Message ----
From: Alex W. <removed_email_address@domain.invalid>
To: removed_email_address@domain.invalid
Sent: Thursday, April 13, 2006 12:23:51 AM
Subject: Re: [Rails] Automatic finder

       First of all the find_by_xxx methods just return 1 record.  I
think the find_all_by_xxx is what you need.

 Does this work?
 chunks = Chunk.find_all_by_category_id(2)

 I'm not entirely sure the dynamic finders work with associations, but
since "category_id" is a direct property of the model it shouldn't have
a problem with it.  Might make a nice patch though.

 -Alex

 Scott W. wrote:    <!-- DIV {margin:0px} -->   It looks rails
automatically creates a "finder" method on models that have a
"belongs_to".  So I tried this in the console:

 c = Category.find(2)
 chunks = Chunk.find_by_category(c)

 chunks is always returned as an empty array.  When I know have some
chunks with a category_id of 2.

 Should the find_by_category do what I think it should be doing or am I
completely off base?

 thanks, cott.

 ----------------------------------------------------------------------------------------------------
 What's an Intel chip doing in a Mac? A whole lor more that it's ever
done in a PC.

   My Digital Life - http://scottwalter.com/blog
   Pro:Blog - http://scottwalter.com/problog


    _______________________________________________ Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
 _______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.