Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append to empty array fails #9

Open
ghost opened this issue Apr 3, 2012 · 2 comments
Open

Append to empty array fails #9

ghost opened this issue Apr 3, 2012 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 3, 2012

>> Request
=> Request(id: integer, inserts: integer_array, updates: integer_array)
>> r = Request.new
=> #<Request id: nil, inserts: [], updates: []>

>> # These array columns have an empty array set as default!

>> r.inserts
=> []

>> # This doesn't work
>> r.inserts << 7
=> [7]
>> r.inserts
=> []

>> # But this does
>> r.inserts += [7]
=> [7]
>> r.inserts
=> [7]

>> # And now, this too
>> r.inserts << 9
=> [7, 9]
>> r.inserts
=> [7, 9]
@Hengjie
Copy link

Hengjie commented Feb 18, 2013

I basically just do r ||= [] before every insert like so:

r ||= []
r << 7

@volonterx
Copy link

volonterx commented Sep 22, 2021

I basically just do r ||= [] before every insert like so:

r ||= []
r << 7

Tried this and got weird bug that in some cases it didn't work.
When I do:

object.array_attribute = []
object.array_attribute << value

than
object.array_attribute # => [value]

But if I do

object.array_attribute ||= []
object.array_attribute << value

than
object.array_attribute # => []

I think in case of ||= [], object.array_attribute have some value, but it is not the []. IDK, maybe this is some serialization issue.

In the end went with old good

object.array_attribute = object.array_attribute << value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants