# How to use my model with conditions

**URL:** <https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592>\
**Category:** Rails\
**Created:** [May 31, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592 "2007-05-31T14:57:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/1 "2007-05-31T14:57:57Z")

</div>

Hello, I am trying to do a custom select for one of my models and I  
can’t seem to get it to work. The thing is, I have a controller named  
Client and a controller named M, with this controller, I want to list  
all the names that start with the letter M. I thought that I would have  
to do this in my model M but now i’m not so sure. In my controller and  
model Client, thats where I create all my clients. But with my  
controller M, i want to get all the names that start with the letter M  
in my table clients.

I want to do “SELECT firstname FROM clients WHERE firstname LIKE ‘M%’”  
in my model named “M”

this is the code in my model M:

belongs\_to :client, :conditions =\> “firstname LIKE ‘M%’”

this is the code in my model Client:

has\_many :ms

this is the code in my controller M:

def index  
@m = Client.find\_all  
end

And here is the code in my view index:

 Listing Names By M
# Names By M:
\<% @m.each do |m| %\> \<%end%\>

| \<%= link\_to m.firstname, :action =\> 'show', :id =\> m.id %\> |

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/2 "2007-05-31T15:02:32Z")

</div>

NVM, I was doing this the wrong way. Sorry

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/3 "2007-05-31T15:16:17Z")

</div>

You cant to add a model and a controller for each letter fo the  
alphabet? That’s definately not DRY at all…  
You should manage every letter in the same controller, and with the  
same model:

- ClientController

- Client (Model)  
#Controller:  
class ClientController \< ActionController::Base

#Model:  
class client ActiveRecord::Base  
…model logic… none nessessary for your example…  
end

#view: index.rhtml

 \<%if @letter %\> Listing Clients with First Name starting with \<%= @letter %\> \<% else %\> Listing all Clients \<% end %\> 
# Names By :
 \<% @clients.each do |client| %\> \<%end%\> 

| \<%= link\_to client.firstname, :action =\> 'show', :id =\> client.id %\> |

 \<%= link\_to "All Names with N", {:action =\> index, :letter =\> "N" } -%\> \<%= link\_to "All Names with O", {:action =\> index, :letter =\> "O" } -%\> .... .... 

Of Course i would use a dropdown to select the letter instead of  
link\_to’s, and do the layout differently, but for an example it should  
suffice.

On 31 Mai, 14:57, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 1, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/4 "2007-06-01T13:33:50Z")

</div>

Well probably something with the find method/conditions it wrong.  
seems it doesn’t return any rows.  
You should check the devlopment.log to see what SWL was generated, us  
script/console to do the find manually and see what it returns…  
Is there really a column “firstname” in you “clients” table?

On 31 Mai, 16:15, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/5 "2007-05-31T16:15:59Z")

</div>

Yes thats what I just figured, anyways yeah I did what you wrote in my  
controller  
now and when i try to access @clients into my view from my controller it  
keeps giving me this error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

This is what I put in my controler Client:

def index  
if params[:letter]  
cond = params[:letter] + “%”  
@letter = params[:letter]  
@clients = Client.find :all, :conditions =\> [“firstname like ?”,  
cond], :order =\> “firstname ASC”  
end  
end

and this is what I put in my view index.rhtml:

 \<%if @letter %\> Listing Clients with First Name starting with \<%= @letter %\> \<% end %\> \<% @clients.each do |client| %\> \<%end%\>

| \<%= link\_to client.firstname, :action =\> 'show', :id =\> client.id%\> |

\<%= link\_to "M", {:action =\> index, :letter =\> "M" } %\>

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 1, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/6 "2007-06-01T13:47:03Z")

</div>

Thorsten wrote:

> Well probably something with the find method/conditions it wrong.  
> seems it doesn’t return any rows.  
> You should check the devlopment.log to see what SWL was generated, us  
> script/console to do the find manually and see what it returns…  
> Is there really a column “firstname” in you “clients” table?
> 
> On 31 Mai, 16:15, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I’ve checked the log, I can’t seem to find anything useful, and yes I do  
have a column named firstname, because if I put my @clients.find outside  
of my “if” declaration, it works.

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [June 1, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/how-to-use-my-model-with-conditions/105592/7 "2007-06-01T14:15:55Z")

</div>

oh… i just see that you left out the “else” statement in the  
controller, which finds all clients names if no :letter is passed in  
the params.  
either you put that back in (and in the view as well) or you create  
some other kind of else method, like redirecting to another page if no  
letter is given.

On 1 Jun., 13:47, Matthew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
