Topic: Missing methods when using :conditions?

Hi, I hope this is the right place to ask this type of 'beginner' question.  I'm following a tutorial on Rails and it was introducing the :conditions and :readonly parameters.  The code example is pulling data from Company based on conditions, setting it to readonly, and assigning the value to 'c':

c = Company.find :first, :conditions => ['state = ?', 'KS'], :readonly => true
c.name = 'Something'
c.save

The example is to show that once it is set as readonly, you should not be able to save changes.  However, I am not able to even call the name method.  When I try the above example, I get:

NoMethodError: undefined method `name' for #<Array:0x1035eb3f8>
    from (irb):44
    from :0

I can test the readonly parameter if I do something like:
c = Company.find :first, :readonly => true

So it seems to be related to applying the condition parameter.  Can anyone explain to me what is going on?  If I look at the c variable, it did find the record and stuff it in the variable so I know the data is there, but I cannot call any of the methods on it.  Is there new changes to rails possibly that changed?

Last edited by derekmw (2010-11-02 12:41:37)

Re: Missing methods when using :conditions?

I'm unable to reproduce this in rails 3.0.1...

c = Company.find :first, :conditions => ['state = ?', 'KS'], :readonly => true

code above returns only one record and (I created three companies with state = 'KS') it responds to .name and raises ReadOnlyRecord error when I try to save it.

NoMethodError: undefined method `name' for #<Array:0x1035eb3f8>

This states that c is a collection of companies (an array) and not a single record. You could try:

c.first.name = "Something"
c.first.save

and see if that raises ReadOnlyRecord error.

Re: Missing methods when using :conditions?

Oddly enough, I stopped the server since then and now that I try to recreate that condition, it works fine.  Not sure why it was failing before or how I got to that point.  I wish I knew to at least  understand how it became an array but I guess for now it will be a mystery.  Thanks for the help either way and I am back on my way to learning rails.