# Were to put a method

**URL:** <https://www.ruby-forum.com/t/were-to-put-a-method/133838>\
**Category:** Rails\
**Created:** [April 4, 2008, 6:10am UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838 "2008-04-04T06:10:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4lfl1ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@h4lfl1ng](https://www.ruby-forum.com/u/h4lfl1ng)\
**Post date:** [April 4, 2008, 6:10am UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838/1 "2008-04-04T06:10:14Z")

</div>

i have this in my view, [Parked at Loopia](http://pastie.caboo.se/175030)  
but it doesnt seem to work.

im not sure what im doing wrong… it think i have the wrong syntax. i  
get  
“no block given” error on line 5

## And what would be the better way to do this? i tried making a method “find\_by(field, item)” but it didnt work out…

View this message in context:  
[http://www.nabble.com/were-to-put-a-method-tp16480986p16480986.html](http://www.nabble.com/were-to-put-a-method-tp16480986p16480986.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![h4lfl1ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@h4lfl1ng](https://www.ruby-forum.com/u/h4lfl1ng)\
**Post date:** [April 4, 2008, 6:21am UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838/2 "2008-04-04T06:21:22Z")

</div>

Try this: (Assuming you have a Model defined as: Prop  
and that model has an attribute called “category”:

\<% Prop.find\_all\_by\_category(“car”).each do |prop| %\>

- &nbsp;\<%=h prop.title %\>
\<% end %\>

-Danimal

---

<div class="post-metadata">

**Author:** ![h4lfl1ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@h4lfl1ng](https://www.ruby-forum.com/u/h4lfl1ng)\
**Post date:** [April 4, 2008, 6:58am UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838/3 "2008-04-04T06:58:52Z")

</div>

Well i tried what you said, and it worked.  
one thing though. I have multiple items in my category column in the  
db, seperated by spaces… and this is what i tried, with no luck if  
there are more than one item.

def find\_all\_by\_category(item)  
self.find(:all, :params =\> {:category =\> /#{item}/})  
end

---

<div class="post-metadata">

**Author:** ![h4lfl1ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@h4lfl1ng](https://www.ruby-forum.com/u/h4lfl1ng)\
**Post date:** [April 9, 2008, 4:55am UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838/4 "2008-04-09T04:55:48Z")

</div>

Just incase anyone was wondering, this is what i came up with after  
more reading of the api and spending some time on freenode, hope it  
helps you.  
def self.find\_all\_by\_category(item)  
self.find( :all, :conditions =\> [“category like ?”, “%#{item}%”] )  
end

---

<div class="post-metadata">

**Author:** ![h4lfl1ng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@h4lfl1ng](https://www.ruby-forum.com/u/h4lfl1ng)\
**Post date:** [April 10, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/were-to-put-a-method/133838/5 "2008-04-10T22:53:16Z")

</div>

Ilya,

That looks good! Glad you got it working. Basically, it’s a way to do  
matches on substrings of category. I.e. if I have a category of “Car”  
I could do: Prop.find\_all\_by\_category(“ar”) and it would match “Car”  
and anything else like that.

The one thing I’d caution you on: this may end up being confusing in  
the future. “find\_all\_by\_X” (where X is some attribute on that model)  
is already part of AR associations. So you are redefining an existing  
method to work differently, which may confuse you or mess you up  
later. My recommendation is to call it something differently, maybe  
like: find\_by\_category\_substring(item).

Just a suggestion though. 🙂

-Danimal
