SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast sax's Avatar
    Join Date
    Feb 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help please - find_all with conditions doesn't work for me

    Hello All,

    For a start, this is my database -

    CREATE TABLE IF NOT EXISTS `client` (
    `id` int(11) NOT NULL auto_increment,
    `ClientID` int(11),
    `LastName` varchar(50) default NULL,
    `FirstName` varchar(50) default NULL,
    `Street` varchar(75) default NULL,
    `City` varchar(75) default NULL,
    `prov_state` varchar(50) default NULL,
    `zip_postal` varchar(10) default NULL,
    `email` varchar(75) default NULL,
    PRIMARY KEY (`ID`),
    KEY `email` (`email`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;

    I have a view page with a form, where there is an input text field as below:

    Code:
    <form method=post action="checkclient" name="loginform">
    
    <input type="text" name="clid" style="border-style:solid; border-color:#CCCCCC;">
    
    <input type=submit onclick="return validateForm()">
    
    </form>
    Here's my Controller -

    Code:
    class ClientinfoController < ApplicationController
    
            model :clientinfo
            scaffold :clientinfo
    
            def index
    		render_text "Boo"
            end
    
            def edit
    		@clientinfo = Clientinfo.find(@params["id"])
            end
    	
            def checkclient
    		userid = params[:clid]
    		@clientinfo = Clientinfo.find_all(["ClientID = ?", userid])
    		render_text @clientinfo.LastName
            end
    		
    end
    I open the above HTML form (loginform), input my id, and gives it to ClientinfoController > checkclient. But this gives me the following error:

    undefined method `LastName' for #<Array:0x3a05610>

    This works though, if I replace @clientinfo.LastName with @clientinfo.inspect, and it throws out the whole array on the page. It doesn't take any of the database parameters. So it's something silly that I've missed out somewhere.

    I've tested another form which takes on the "edit" method above, and that works fine for me.

    I'm using Webbrick on my local system - haven't tried this piece on Apache yet.

    Could you guys help me on this please?

    Sax.
    "Be Still Like A Mountain And Flow Like A Great River!"

    Cooking With Your Heart And Soul

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried:
    Code:
    @clientinfo = Clientinfo.find(:all, :conditions => ["ClientID = ?", userid])
    http://api.rubyonrails.com/classes/A...e.html#M000715

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you do a find all, you end up with an array, not a single record, even if none or just one record is found. You are trying to coax a characteristic of one object out of a list of potentially many objects.

    If you want just one record, do a "first" find.
    Whatever.find(:first, :conditions => ['foo=?', bar])

    Try render :text => debug(@clientinfo)
    or: render :text => @clientinfo.first.LastName
    or kick the array over into a template and do something like:

    Code:
    <ul>
      <% @clientinfo.each do |ci| %>
        <li><%= ci.LastName %></li>
      <% end %>
    </ul>
    Or better yet test this out using script/console where you get step by step feedback.

    Quote Originally Posted by sax
    I'm using Webbrick on my local system - haven't tried this piece on Apache yet.
    In this case it doesn't matter, not even a little.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Enthusiast sax's Avatar
    Join Date
    Feb 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Sam,

    Thanks for that. That was the issue - it works now.

    Reggie,

    Thanks for your comment. I had tried that already, but it didn't work. It was giving me an array.

    I think I'll need to go through the API more.

    Thanks for the help guys, appreciate it very much.
    "Be Still Like A Mountain And Flow Like A Great River!"

    Cooking With Your Heart And Soul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •