Forum: Ruby on Rails AR: freezing PG Array

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
96fff1d27a94f6b14cb3d11e4a21b417?d=identicon&s=25 Jan B. (mrbrdo)
on 2014-03-13 10:08
(Received via mailing list)
Wouldn't it make sense to freeze the array object that is returned by
ActiveRecord when you have an array in Postgres? That would prevent
accidentally changing the array and forgetting to mark it as dirty.
The point being that changing the array does not register with AR and so
is not saved to DB upon calling save.
Josh Jordan (Guest)
on 2014-03-14 14:08
(Received via mailing list)
If that is true, the solution would be to properly mark the object as
dirty when the array changes, not freeze the array. Why should this data
type behave differently than all the others?
96fff1d27a94f6b14cb3d11e4a21b417?d=identicon&s=25 Jan B. (mrbrdo)
on 2014-03-14 14:11
(Received via mailing list)
That would mean monkey-patching the Array instance that is returned or
wrapping it (and also doing that if something new is assigned). I'm not
against it, but I think the general consensus is to avoid stuff like
in core Rails. (Guest)
on 2014-03-14 14:18
(Received via mailing list)
I don't see why that's true. It would be simple enough to use a proper
solution, such as writing a class that has the same interface as an
but tracks changes, or to extend Array instances with a module that does
the same.
96fff1d27a94f6b14cb3d11e4a21b417?d=identicon&s=25 Jan B. (mrbrdo)
on 2014-03-14 14:33
(Received via mailing list)
I remember there was some discussion at some point in time about doing
something like that and it was dismissed for some reason, but I could be
Of course that would be ideal, to track the dirty state. On the other
freezing the array is much easier and less complex to implement so it
at least be the first step, if we want to see it in Rails any time soon.
think we do need something, because right now it's really prone to user
HStore also has this issue I think.

Don't get me wrong I think wrapping the array is the better long-term
solution, if it's done the right way, but since it's a bit more complex
might take longer to get it upstream.
Matt Jones (Guest)
on 2014-03-14 16:48
(Received via mailing list)
On Friday, 14 March 2014 09:32:05 UTC-4, Jan Brdo wrote:
In general, the Rails philosophy is "trust that the developer knows what
they're doing", not "hide all the sharp bits because somebody might get
hurt". Don't expect the framework to keep you from shooting yourself in
foot, write tests that verify you are aiming in the right direction.

--Matt Jones
This topic is locked and can not be replied to.