Skip to content

AR not save updated PostgreSQL arrays #14763

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

Closed
nleo opened this issue Apr 15, 2014 · 4 comments
Closed

AR not save updated PostgreSQL arrays #14763

nleo opened this issue Apr 15, 2014 · 4 comments

Comments

@nleo
Copy link

nleo commented Apr 15, 2014

pry> model.array << element
pry>  model.save
   (0.4ms)  BEGIN
   (0.4ms)  COMMIT
=> true
unless File.exist?('Gemfile')
  File.write('Gemfile', <<-GEMFILE)
    source 'https://rubygems.org'
    gem 'rails', '4.1.0'
    gem 'pg'
  GEMFILE

  system 'bundle'
end

require 'bundler'
Bundler.setup(:default)

require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'pry'

ActiveRecord::Base.establish_connection adapter: "postgresql",
  host: "localhost",
  user: "postgres",
  database: "test"

ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  execute 'drop table if exists bars'
  create_table :bars
  execute "alter table bars add column array_of_varchar varchar(255)[]"
end

class Bar < ActiveRecord::Base
end

class BugTest < Minitest::Test
  def test_postgresql_save_array
    bar = Bar.create! array_of_varchar: []
    bar.array_of_varchar << 'Sample string'
    bar.save!
    bar.reload
    assert_equal 'Sample string', bar.array_of_varchar[0]
  end
end
@nleo
Copy link
Author

nleo commented Apr 15, 2014

#not work
model.update! array: model.array.push(element)
#works
model.array << element
model.array_will_change!
model.save!

@senny
Copy link
Member

senny commented Apr 15, 2014

@nleo this is currently expected behavior. Rails does not know when you change the object itself. You need to use the attribute writer array_attribute= or the array_attribute_will_change! method.

@senny senny closed this as completed Apr 15, 2014
@senny
Copy link
Member

senny commented Apr 15, 2014

It's also the same as with #6127

@nleo
Copy link
Author

nleo commented Apr 15, 2014

@senny I agree it maybe overwork to watch array changes, just notice that as user then I say to system update! and specify attribute and value I expect to overwrite columne whenether it changed or not.

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