Wednesday, May 27, 2009

Read only models in ActiveRecord

I was working in one of my project, where we were required to connect to 2 databases.
  1. First we had access to do CRUD
  2. Second we were only supposed to READ
For the second DB we wanted to raise exception from Rails, if somebody by accident tried to create/edit. ActiveRecord has this attribute as part of all models already called @readonly. There are definitely few challenges. Here is what I did:

The sweet model:


If somebody tries to add a record:


If someone tries destroy


What if someone uses delete


Rest in peace !!

No comments: