10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveModel::Attributes.attribute で配列を使う

Last updated at Posted at 2018-10-23

概要

仕事でActiveModelを利用した時にはまった内容。

内容

やりたかったこと

app/model/hoge.rb
class Hoge
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :fuga, :array # 配列で保持できるようにしたい
end

はい、無理でした。。
そもそもActiveModel::Typeで配列は定義されていない。。(ref

どうしたか

とりあえずsourceを見つつ以下のようにしてみたら動いた

app/model/hoge.rb
class Hoge
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :fuga, array: :integer
end

とりあえずは動いたが...

本当にこれでいいのかちゃんと調べないとなぁ...

10
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?