Topic: Undefined method?

I have a table called surveys. I made a scaffold for that. Now I want to find a specific row that has a certain value before a new can be made. In the new action I have:

def new
    @asurvey = Survey.find(:all, :conditions => "status='Active'")
    @survey = Survey.new
end

But in the page new.rhtml when I try @asurvey.id I get a big number instead of what I expect. And if I try to get another field like @asurvey.status I get an error "undefined method `status'". Why is that?

[OT] - I'm not sure RoR was such a smart move after all. I find myself struggling to find answers so much that I almost think I might go back to PHP. Is there a better manual for RoR in the works? Now it is far from the simplicity and elegance that PHP has.

Re: Undefined method?

It works better when I use :first instead of :all. But shouldn't :all also work? There's only one row in the table anyway.

Re: Undefined method?

Ruby and RoR have a rather high learning curve, but the payoff is worth it. Once you get the hang of it you will be able to make webapps that run circles around PHP and in a fraction of the time.

If you call find with the :all parameter, it's returning all entries that match the conditions, so it's returning an array of entries.

If you just want the first entry that meets your condition, use :first

@asurvey = Survey.find(:first, :conditions => "status='Active'")

Or for slightly cleaner code, try

@asurvey = Survey.find_by_status('Active')

Re: Undefined method?

palb wrote:

It works better when I use :first instead of :all. But shouldn't :all also work? There's only one row in the table anyway.

In that case it returns an array with one entry in it. Most of the time you have no idea how many entries you'll get back, so your code always wants an array returned. If one entry instead returned just that entry, that'd be a ripe pain, you'd have to say "if i just got one back, do this, otherwise, do that", yuck.

find_by_<column_name> and find_all_by_<column name> are generally the better ways to do this anyway.

Last edited by tortoise (2007-01-02 12:04:00)

Re: Undefined method?

tortoise wrote:

Ruby and RoR have a rather high learning curve, but the payoff is worth it. Once you get the hang of it you will be able to make webapps that run circles around PHP and in a fraction of the time.

If you call find with the :all parameter, it's returning all entries that match the conditions, so it's returning an array of entries.

If you just want the first entry that meets your condition, use :first

@asurvey = Survey.find(:first, :conditions => "status='Active'")

Or for slightly cleaner code, try

@asurvey = Survey.find_by_status('Active')

Thanks. I understand now.

I get a really good feeling when it's so simple like Survey.find_by_status. And that's why I'm hanging in there. :-)

I must say that Ruby in itself is really simple compared to PHP. The other languange I know, besides plain Javascript, xhtml, css and that stuff, is Lasso. That is also very simple (not sure it will survive though), more so than PHP, but not like Ruby. Well, kind of in the same league. The thing with RoR is the MVC structure that I feel gives me more overview.

What I like about PHP, and even more in Lasso, is the manual. In this sense RoR is a mystery to me. Everything else is about simplicity and elegance, including many sites I've seen built in Rails, but not the way it's explained. I agree it's a steep learning curve. However, it could be very different.

Re: Undefined method?

I hear you. I've had to do a lot of experimenting to understand how things work. That plus AWDwR book, google searches, the API and random blog posts has gotten me through. A solid manual with all this information in one place is needed.

Railscasts - Free Ruby on Rails Screencasts

Re: Undefined method?

Btw, how do you check if Method.find_by_column finds a row? With @result.size or .count or other?

Re: Undefined method?

I check it using if @result, because if it returns no rows then @results may be null, not sure if thats the best way

Re: Undefined method?

Yeah, that's a good way. If you are doing find(:first) or find_by... it will return nil if no result is returned. However, if you are doing find(:all) or find_all_by... it will return an empty array so you need to call @results.empty?.

Railscasts - Free Ruby on Rails Screencasts