toamitkumar's Code Blog

Ruby, Rails, RubyMotion, Chef, Puppet, Play and others. Opinions expressed here are my own and do not reflect those of my employer.

Read Only Models in ActiveRecord

| Comments

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 !!

Comments