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: <html> <head> <title>Listing Names By M</title> </head> <body> <h1>Names By M:</h1> <table> <% @m.each do |m| %> <tr> <td><%= link_to m.firstname, :action => ''show'', :id => m.id %></td> </tr> <%end%> </table> </body> </html> Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
NVM, I was doing this the wrong way. Sorry -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <html> <head> <%if @letter %> <title>Listing Clients with First Name starting with <%@letter %></title> <% else %> <title>Listing all Clients</title> <% end %> </head> <body> <h1>Names By :</h1> <table> <% @clients.each do |client| %> <tr> <td><%= link_to client.firstname, :action => ''show'', :id => client.id %></td> </tr> <%end%> </table> <%= link_to "All Names with N", {:action => index, :letter => "N" } -%> <%= link_to "All Names with O", {:action => index, :letter => "O" } -%> .... .... </body> </html> 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 Lagace <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > <html> > <head> > <title>Listing Names By M</title> > </head> > <body> > <h1>Names By M:</h1> > <table> > <% @m.each do |m| %> > <tr> > <td><%= link_to m.firstname, :action => ''show'', :id => m.id %></td> > </tr> > <%end%> > </table> > </body> > </html> > > Thanks > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: <html> <head> <%if @letter %> <title>Listing Clients with First Name starting with <% @letter %></title> <% end %> </head> <body> <table> <% @clients.each do |client| %> <tr> <td><%= link_to client.firstname, :action => ''show'', :id => client.id%></td> </tr> <%end%> </table> <%= link_to "M", {:action => index, :letter => "M" } %> </body> </html> Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Lagace <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > <html> > <head> > <%if @letter %> > <title>Listing Clients with First Name starting with <%> @letter %></title> > <% end %> > </head> > <body> > <table> > <% @clients.each do |client| %> > <tr> > <td><%= link_to client.firstname, :action => ''show'', :id => > client.id%></td> > </tr> > <%end%> > </table> > <%= link_to "M", {:action => index, :letter => "M" } %> > </body> > </html> > > Thanks > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Lagace <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Lagace <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Lagace <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---