AR: freezing PG Array

26 views
Skip to first unread message

Jan Brdo

unread,
Mar 13, 2014, 5:06:40 AM3/13/14
to rubyonra...@googlegroups.com
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 it is not saved to DB upon calling save.

Josh Jordan

unread,
Mar 14, 2014, 9:07:46 AM3/14/14
to rubyonra...@googlegroups.com
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?

Jan Brdo

unread,
Mar 14, 2014, 9:10:33 AM3/14/14
to rubyonra...@googlegroups.com
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 that in core Rails.

josh....@gmail.com

unread,
Mar 14, 2014, 9:17:25 AM3/14/14
to rubyonra...@googlegroups.com

I don't see why that's true. It would be simple enough to use a proper OO solution, such as writing a class that has the same interface as an Array but tracks changes, or to extend Array instances with a module that does the same.

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/fnpTlBZ-lD4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4bd3927b-e335-4b5c-b631-80f7e21cee0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Brdo

unread,
Mar 14, 2014, 9:32:05 AM3/14/14
to rubyonra...@googlegroups.com
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 wrong.
Of course that would be ideal, to track the dirty state. On the other hand freezing the array is much easier and less complex to implement so it could at least be the first step, if we want to see it in Rails any time soon. I think we do need something, because right now it's really prone to user error.
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 it might take longer to get it upstream.

Matt Jones

unread,
Mar 14, 2014, 11:46:38 AM3/14/14
to rubyonra...@googlegroups.com


On Friday, 14 March 2014 09:32:05 UTC-4, Jan Brdo wrote:
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 wrong.
Of course that would be ideal, to track the dirty state. On the other hand freezing the array is much easier and less complex to implement so it could at least be the first step, if we want to see it in Rails any time soon. I think we do need something, because right now it's really prone to user error.
HStore also has this issue I think.

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 the foot, write tests that verify you are aiming in the right direction.

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages