Storing comma separated list in db column, how to get and set?
I want to store a comma separated list in a database column like:
234234,2134234,2453245,2345
I then want to get this list, and use it in a query like:
u = User.f开发者_开发技巧ind("where some_id in (?)", my_comman_seperated_list)
I have to convert this into an array then right?
Define your column as text
, and in ActiveRecord declare that the column as serialized:
serialize :my_column
Now that column will be stored as [234234,2134234,2453245,2345]
, and when you get that object, the attribute will be an array you can pass into your find.
Is there are a reason this must be in a column, rather than as a separate table? It might make more sense to create a new model for the data being stored in that column, and then store one value in each row, which belongs_to :user
.
精彩评论