Array Attribute for Ruby Model

前端 未结 5 2111
情书的邮戳
情书的邮戳 2021-01-30 14:19

Is it possible to create an attribute for a class that is an array? I tried reading this but I didn\'t get much out of it. I want to do something like this:

clas         


        
5条回答
  •  无人及你
    2021-01-30 14:54

    If using Postgres, you can use its Array feature:

    Migration:

    add_column :model, :attribute, :text, array: true, default: []
    

    And then just use it like an array:

    model.attribute # []
    model.attribute = ["d"] #["d"]
    model.attribute << "e" # ["d", "e"]
    

    This approach was mentioned by Marnen but I believe an example would be helpful here.

提交回复
热议问题