0

I read all about this and how there are transactional issues in updating an array column but mine wont even create.

class Recipe < ActiveRecord::Base
  serialize :steps
  ...
end

Migration

add_column :recipes, :steps, :text, array:true, default: []

Output

$ r = Recipe.create(steps: [1,2,3])
> #<Recipe:0x007fe38ae68870 id: 27, ..., steps: [], ingredients: []>

Also played around with this method with no luck:

def add_steps(steps)
  steps_will_change!
  update_attributes steps: self.steps + steps
end
6
  • does it matter that you have a type text and are saving integers? Commented Mar 1, 2016 at 22:55
  • I don't think so. It will be strings that I'm saving and I tested it with ["a", "b", "c"] and no luck either. Commented Mar 1, 2016 at 22:59
  • 1
    You are testing in a console. Have you tried reload! in the console? Commented Mar 1, 2016 at 23:05
  • 2
    Executive Summary: Don't use serialize with PostgreSQL array columns. Commented Mar 1, 2016 at 23:09
  • 2
    @muistooshort was right. No serialize with array columns. It's one or the other. Thanks Commented Mar 1, 2016 at 23:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.