1

Rails 4 with Postgres 9.2. 'pg' gem version 0.16.0.

Model attribute looks like this in schema.rb:

t.string "codes",  array: true

I'm trying to store the following array of strings in this field:

["", "a string"]

In the database table, data for this field is stored as {"","a string"} which is what I expect.

However, in Rails, retrieving this attribute via model.codes strips out the first element in the array and returns just:

["a string"]

a) What happened to the (intentionally-placed) empty string I had in the array?

b) If I replace the empty string, with a single space character, the retrieved attribute looks fine ([" ", "a string"]), but I'm hoping not to have to resort to that.

2
  • Did the links help? Does serializing the data solve your problem? Commented Aug 11, 2013 at 12:15
  • Sadly, not quite. The accessed ActiveModel entities are no longer an accurate representation of stored data, which is the real crux of my question. Admittedly, serialization could potentially make my code operational, but skips addressing the root issue of Rails retrieving db data not as persisted. 'More native' Rails support for PG data array types was a reason for upgrading to Rails 4. Commented Aug 13, 2013 at 21:35

2 Answers 2

2

After much digging, I have something that should be useful for people searching for solutions to the same problem.

The Postgres adapter in Rails relies on an external gem pg_array_parser for the heavy lifting of efficient array parsing, and defaults processing to it if this gem is available in a project.

The issue listed above does not recur when adding this gem to my bundle, and all data access/persistence behaves as expected without stripping out empty strings from the array.

Relevant line in Rails:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb#L18

Sign up to request clarification or add additional context in comments.

2 Comments

So, is this a bug in Rails? How does it do the parsing in the absence of pg_array_parser?
Yup, @dukedave. It's a Rails bug. In the Rails master file posted in the answer, default array processing in the absence of pg_array_parser goes into a local method named parse_array_contents. I haven't had a chance to spin through it and submit a Github issue yet. It's a case statement within a while loop that iterates over the array, so should be easy to follow. From what I see in the codebase, I speculate that the Rails/pg team intentionally prefers to offload array parse responsibility to the gem, which is what I advise too for your project, if possible.
0

These articles might help:

  1. Storing arrays in database using ActiveRecord
  2. rails store array in to database field
  3. http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-serialize

It looks like you need to serialize the column:

class MyStuff < ActiveRecord::Base  
  serialize :things
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.