user531065
user531065

Reputation: 763

rails query serializable object

How do I query the database to find objects that contain one or more attributes that are stored as serializable?

For example, I have a concert which occurs only in certain cities. I want to make a Concert object with a column called cities and store an array of cities.

If I want to query my database to find all concerts that occur in 1 city (or all concerts that occur in an array of n cities), how do I do this?

Upvotes: 0

Views: 650

Answers (2)

lulalala
lulalala

Reputation: 17981

One possible way to query would be to use SQL's LIKE condition. This would work for boolean conditions in serialized tables.

For example to find those Users with the 'notification' option on,

users=User.arel_table
User.where(users[:options].matches("%notification: true%"))

As for other type of variables this would not be as feasible.

Upvotes: 0

jimworm
jimworm

Reputation: 2741

The best way to do this isn't to store it in a serialized column, but a separate table called Cities. Then you can do this:

City.find_by_name('Cityname').concerts

Upvotes: 2

Related Questions