In the model I serialize the column category in Event model to be array, so i have
serialize :category
The thing is, i need to provide search function to users. So how do i search if a category is inside an event.
Event.find(:all, :conditions => ['category = ?', params[:category]])
This won’t work as the category is stored as serialized array. Any idea?
The only way i can think of get all the events and filter out each instance.
Event.all.select{|e| e.category.include? params[:category]}
This is not efficient at all.
Or else, i can use like statement
Event.find(:all, :conditions => ['category = ?', "%-#{params[:category]}%"])
I would create a separate table and model for categories. Then you can get all events in a category by using a has_many association.