NoMethodError ... nil.clear

Hi,

Rails 2.3.5

Any ideas on the following error when calling create on an
ActiveRecord object?

NoMethodError (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.clear):

The create method is inside a Module that I want each of my Model
classes to include. All of my DB tables follow the same structure, so
I am trying to factor out the updating and searching of the tables to
just a few Modules. My DB structure should allow it with a little
clever coding.

(Note: just finished reading Metaprogramming Ruby from PragProg. It
has taught me a fair bit (still loads more to learn I guess), but I
now want to refactor a current project as far as I can as a learning
excercise).

If I include the Modules in the Controllers so that each Controller
gets all the functionality of the Modules, then the Controllers act
‘on’ the Model Classes and the create method succeeds, but when I move
the Module to the Model, which is where it should really be so that
the Model Class gets the methods itself, I get this error.

Have I missed something with ‘self’?

The Class that calls create does exist, and the params passed to
create are correct.

(More info available if required to help fix)

Many Thanks

Paul

On 23 February 2010 23:49, paul h [email protected] wrote:

NoMethodError (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.clear):

If you post code extracts from around the area that seems to be
failing, we might be able to help.

Have I missed something with ‘self’?

I’ve no idea, that’s the first time you’ve mentioned it, so we don’t
know what kind of grasp you think you have to be able to suggest
anything you may have missed.

just finished reading Metaprogramming Ruby

That might be a little bit beyond the position you’re at at the
moment, maybe a book that offers a better grounding would be more
appropriate; like “Ruby for Rails” (http://www.manning.com/black/)

Thanks for the reply, but figured it out.

I didn’t give much helpful info, I know, it was the nil.clear that I
couldn’t find any info on anywhere, spent ages searching and was
hoping it might just twig something with someone.

The problem was, I was overwriting model methods I shouldn’t with one
of my modules.

I understand OO, MVC, and don’t intend to continue writing, what was
basically, a bunch of boiler plate code in my models due to the DB
tables following the same formats, so I am refactoring with the help
of Metaprogramming Ruby (after having spent a lot of time with AWDwR
and Pickaxe + a few others). It has taught me a lot about Ruby, and
has helped me reduce masses of lines of code into just a few small
modules - just got caught by unknowingly MonkeyPatching.

Thanks again

Paul

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