Perhaps you should detach the os_usage array from your model and make it a separate table.
In the ActiveRecord world, you get something like the following code:
class MentorData < ActiveRecord::Base .. has_and_belongs_to_many :os_usage .. end class OsUsage < ActiveRecord::Base .. has_and_belongs_to_many :mentors_data .. end
Creating the many_to_many relationship between the two models makes it easy to query and avoid duplication. This method is called normalization.
Using this new design, you have an os_usage collection created by objects instead of strings
MentorData.first.os_usage # => [#<OsUsage:....>, #<OsUsage:...>]
What you can easily convert to an old string array
MentorData.first.os_usage.map(&:name)
In addition, you can request data for all MentorData that include os_usage from apple:
MentorData.joins(:os_usages).where('os_usages.name' => 'apple')
And also request all the MentorData entries for OsUsage:
OsUsage.where(name: 'apple').mentors_data
Hope you find this helpful :)
source share