views:

147

answers:

1

Hello,

I'm trying to create a rails web app that does not use ActiveRecord framework as I have no relational database back end.

So far I managed to create a simple example application that works perfectly for listing, showing and editing my records.

However I'm facing some problems when it comes to the record creation.

The issue is that the controller class does not behave in the same manner if the model class does not inherit from ActiveRecord::Base.

From the controller's point of view, in a classic active record model schema where the model inherits from ActiveRecord::Base, the creation of a record seems to follow this sequence :

  • controller.new : creation of a non persistent record, rendering the input form
  • controller.create : set up the record with user input, persistant storage , ….

However in the case where the model is not a ActiveRecord we get the following sequence :

  • controller.new
  • controller.update

Now of course this is a little bit strange because we're skipping the creation method so it will lead to a nil instance error !

I'm pretty certain that this behavior is dictated by something that should be implemented in the model, however I was unable to find what (probably because I'm an absolute beginner with rails )

So if anyone here has some pointers on what I should be looking/implementing in order to solve this situation and have the same behaviour in both cases, I'd be realy grateful.

Thanks,

+4  A: 

Have your custom model implement the MyModel#new_record? method (instance method) and you will have the desired behavior. When new_record? returns true, it will go to create, and when it returns false it will go to update.

clyfe
Works great ! Thank you very much ! BTW is there a doc book or spec that provides such information ? Most of the book I've checked are related to the "classic" usage with active record and don't realy cover the internals of the rails processing. Thanks again
devlearn
I don't think so, i learned this stuff on the go, but one can always check the sources, and netbeans's visual debugger helps a lot.
clyfe
This is the answser I was afraid of ;). Thank you
devlearn