LoginSignup
1
0

More than 3 years have passed since last update.

Ecto の virtual field で struct を参照する

Posted at

Ecto.Schema のフィールドを定義するときに、struct を参照する virtual なフィールドを定義したい。

field :my_struct, MyStruct, virtual: true

しかし、こうするとコンパイルエラーになってしまう。

== Compilation error in file ... ==
** (ArgumentError) schema MyStruct is not a valid type for field :my_struct. Did you mean to use belongs_to, has_one, has_many, embeds_one, or embeds_many instead?

かといって、has_one/3 などのリファレンスを読んでも virtual オプションはない。結局、タイプを :any とすることで切り抜けたが、型を明示するためにもできれば struct を指定したいところ。

field :my_struct, :any, virtual: true
1
0
0

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
1
0