rails: Unable to create an attribute for an array

Steps to reproduce

Easiest way is to enter the following in Rails console:

ActiveRecord::Type.lookup(:integer, array: true)

From a model, you can do this:

  attribute :my_int_array, :integer, array: true

Expected behavior

According to the documentation of attribute method, it should be possible to specify an attribute that is an array.

  attribute :my_int_array, :integer, array: true

Actual behavior

Seeing this error:

ArgumentError: unknown keyword: array
from /Users/newuser/.rvm/gems/ruby-2.3.3@citiesense/gems/activemodel-5.1.5/lib/active_model/type/value.rb:6:in `initialize'

System configuration

Rails version:

5.1.5

Ruby version:

2.3.3

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

I’m unsure where to ask this, but, on the offchance anyone reads this…

I’ve been using ActiveModel::Attributes for form objects (replacing Virtus). It really works nicely.

It would be wonderful to be able to do

class Foo
  include ActiveModel::Model
  include ActiveModel::Attributes
  attribute :bars, :string, array: true
end

Unfortunately, we’re using mysql not postgres. It seems to me (probably very incorrectly) that ActiveModel::Attributes don’t really need to be backed by the database, at least, I’m just using it for coercing values and implementing specific rules for certain forms.

Is this a feature that’s possibly going to built out? (or is there a nice workaround)

@rafaelfranca why does an ActiveModel attribute need to be backed by a database column to be an array? there are tons of cases where I would use it without postgresql if I was using ActiveModel on its own…, plus the attributes api is not only for DB backed attributes

it works like this attribute :my_array, array: true

For the array support, for sure it needs to be backed by a column. For other cases it doesn’t need to be.

Thank you. I guess that wasn’t clear to me from reading the documentation.

Volkan Unsal Product Engineer volkanunsal.com