# Find with dynamic attribute name

**URL:** <https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921>\
**Category:** Rails\
**Created:** [December 22, 2007, 1:49am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921 "2007-12-22T01:49:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 1:49am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/1 "2007-12-22T01:49:26Z")

</div>

The model Teacher has attributes of :name, :period1, :period2, :period3

In a part of the view I am iterating with an “upto” clause and have  
made an instance variable named “hold\_period” that has a strings like  
“period1” , “period2”

I am successfully using things like teacher.send(hold\_period) to access  
period column data from a given teacher instance.

What I want to do now is search a whole collection of teachers and find  
matches to a string like “on break” in the attribute column stored in  
hold\_period.

If I knew it was period1 I would go

@on\_break = @teachers.find\_by\_period1(“on\_break”)

but I don’t think I can go

@on\_break = @teachers.find\_by\_(hold\_period)("on\_break’)

How can I find all the teachers with “on break” in attribute column name  
stored in the variable hold\_period ?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 1:59am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/2 "2007-12-22T01:59:01Z")

</div>

Can you not just query the database with the correct where statement? I  
cant think of a good reason why you wouldn’t be able to do it…

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 2:49am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/3 "2007-12-22T02:49:59Z")

</div>

Tim P. wrote:

> Can you not just query the database with the correct where statement? I  
> cant think of a good reason why you wouldn’t be able to do it…

I was afraid I’d get that sort of answer ! lol

The problem is I don’t know anything about databases except what I’ve  
learned by learning what I got so far from rails. I’d prefer to keep the  
code in rails rather than database specific queries.

I think I’m finding the answer on page 303 of the AWDwR book find\_by\_sql  
but I’m having a hard time understanding whats going on there (probably  
because I’m not quite clear on what “select” and “where” do and even  
more unclear of how to limit the search to only the collection at hand  
and to use the column named in the variable).

I guess there’s no time like the present to crack open that dusty mysql  
in 24 hours book I never really got through. (I’d still appreciate  
someone taking a stab at the example I laid out)

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 3:03am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/4 "2007-12-22T03:03:25Z")

</div>

On 22 Dec 2007, at 00:49, Tom N. wrote:

> period column data from a given teacher instance.  
> but I don’t think I can go
> 
> @on\_break = @teachers.find\_by\_(hold\_period)("on\_break’)

Why the dynamic finder obsession?

@on\_break = @teachers.find :all, :conditions =\> {hold\_period =\>  
“on\_break”}  
should do the trick (you might have to convert hold\_period to a  
symbol, can’t remember off the top of my head.

Fred

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 3:37am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/5 "2007-12-22T03:37:54Z")

</div>

On 22 Dec 2007, at 02:16, Tom N. wrote:

> > Why the dynamic finder obsession?
> 
> I dunno… seems pretty silly in hindsight.

for the record, if you absolute had to do it that way, you can always do  
Foo.send(“find\_by\_#{attribute\_name}”, attribute\_value)

That said, I don’t think there’s much point. Dynamic finders increase  
readibility compared to find :all, :conditions =\> ‘…’, but once you  
start mucking around with send and constructing the method name on the  
fly you’ve lost that readability advantage.

Fred

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 3:16am UTC](https://www.ruby-forum.com/t/find-with-dynamic-attribute-name/123921/6 "2007-12-22T03:16:49Z")

</div>

> Why the dynamic finder obsession?

I dunno… seems pretty silly in hindsight.

Thanks Fred, looks like I needed to back up a page in the book and looks  
like I might be over using find\_by\_xxx too.
