How to use my model with conditions

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

NVM, I was doing this the wrong way. Sorry

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

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

#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. [email protected]

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. [email protected]

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

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. [email protected]

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.

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. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs