A
A
Anton2014-04-21 16:09:27
Ruby on Rails
Anton, 2014-04-21 16:09:27

How to filter a field saved from select multiple?

Good afternoon!
The Person model has a year property. When editing a model, this field is filled in from

<%= f.select(:pdate, (@years), {:include_blank => false}, {:multiple => true}) %>

The model says In the database, everything is perfectly saved in the form of YAML:
serialize :year;
---
- '2014'
- '2011'

Question: how to filter by one of the values ​​of this field in the active record now? Let's say I want all the people who participated in 2011. Now I do this:
@year = 2011.to_s
@people = Person.where('year like ?', "%#{@year}%").all

Is there any more correct way?
rails version - 3.2.17

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
himik, 2014-04-28
@h8every1

incorrectly designed. or use a separate table to store years, or a database that supports arrays natively, postgres for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question