Is there a way to enforce read-only access on an ActiveRecord object? I want to be able to modify the AR object, but I don''t want it *ever* to be saved to the DB. I know there are a few ways, such as only granting that MySQL user read only access (no inserts or updates), or I can manually override the save() method ... but I only want this to be a temporary thing. Sort of like: ActiveRecord::Base.set_readonly Which can then be manually reversed by ActiveRecord::Base.set_fullaccess Just curious. Thanks! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi You could use a callback for. Have a look here: http:// ar.rubyonrails.com/classes/ActiveRecord/Callbacks.html Regards Manuel Holtgrewe
On 29 Sep 2005, at 23:27, Belorion wrote:> Is there a way to enforce read-only access on an ActiveRecord object? > I want to be able to modify the AR object, but I don''t want it *ever* > to be saved to the DB. > > I know there are a few ways, such as only granting that MySQL user > read only access (no inserts or updates), or I can manually override > the save() method ... but I only want this to be a temporary thing. > Sort of like: > > ActiveRecord::Base.set_readonly > > Which can then be manually reversed by > > ActiveRecord::Base.set_fullaccessOr some combination of both. Just an idea, not tried this: def toggle_read_only # start as if saving is allowed @read_only ||= false #toggle read only @read_only = !@read_only end def save unless @read_only super else ... # something else here? end end Mike
On Sep 29, 2005, at 3:27 PM, Belorion wrote:> Is there a way to enforce read-only access on an ActiveRecord > object? I want to be able to modify the AR object, but I don''t > want it *ever* to be saved to the DB.The Ruby-esque way to make something immutable is via .freeze - though I have no idea if that''ll work with an instance of ActiveRecord. --Steve
Stephen Waits wrote:> > On Sep 29, 2005, at 3:27 PM, Belorion wrote: > >> Is there a way to enforce read-only access on an ActiveRecord >> object? I want to be able to modify the AR object, but I don''t want >> it *ever* to be saved to the DB. > > > The Ruby-esque way to make something immutable is via .freeze - though > I have no idea if that''ll work with an instance of ActiveRecord.That''d do the opposite, surely - the AR object couldn''t be changed, but the save method would still work. There are times when you''d want that functionality, too, so I don''t think freeze should be overridden. Quick and easy: swap out the connector for an instance of an abstract connection adapter, so that the AR object forgets how to update the database, or just override the save method (or create_or_update) with something harmless. Proper: check the role of the user at save time against a permission field in the table. -- Alex
Freeze works (I think Rails uses it on objects whose records have been destroyed?) and was my first thought, but Belorion wants to be able to change the AR object, but to interrupt any attempt to save to the database - so it''s the database record he wants to be ''read only''. On 30 Sep 2005, at 01:16, Stephen Waits wrote:> > On Sep 29, 2005, at 3:27 PM, Belorion wrote: > >> Is there a way to enforce read-only access on an ActiveRecord object? >> I want to be able to modify the AR object, but I don''t want it >> *ever* to be saved to the DB. > > The Ruby-esque way to make something immutable is via .freeze - though > I have no idea if that''ll work with an instance of ActiveRecord. > > --Steve > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sep 30, 2005, at 11:36 AM, Michael Houghton wrote:> Freeze works (I think Rails uses it on objects whose records have > been destroyed?) and was my first thought, but Belorion wants to be > able to change the AR object, but to interrupt any attempt to save > to the database - so it''s the database record he wants to be ''read > only''.Someone mentioned callbacks earlier. # Fail fast (good) class Foo < ActiveRecord::Base class ReadOnly < ActiveRecord::ActiveRecordError; end before_save { raise ActiveRecord::ReadOnly } end # Silently ignore (evil) class Foo < ActiveRecord::Base before_save { false } end Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDPaHUAQHALep9HFYRAlSDAKCrwgrRSr1Af3RKxOUJSnFRhKszhACglWgA KhRXD6mdOlxB5d78nRlLBkU=xBjE -----END PGP SIGNATURE-----