Actually, this is what I would do if I had to deal with 8-10 attributes. But if you have too many attributes for the Profile model class and you have complex logic for displaying these attributes based on a user group, public, common, etc., then I would advise you to move this to a separate model class, say: " ProfileConfiguration or ProfileSetting ", which will support each attribute at the row level, or you can move these settings to Redis, where the structure will look like: user_id: {attribute_name: true, type: 'type_name'} , but then there is a drawback that you will depend from the availability of the Redis server.
Now, in your case:
serialize :profile_preferences, Hash
and then you support it (as you mentioned, just the opposite for their purpose):
{email: false, address: false, age: true }
However, you can continue and create some convenient methods that you can call in your profile object:
after_initialize :load_profile_preferences private def load_profile_preferences profile_preferences.each do |attr, value| self.class.send(:define_method, "show_#{attr.to_s}?") { value } end end
Now will you get handy methods like show_email? show_address? and show_age? on Profile a class object that you can delegate to a User instance of the class. So now you can do something similar in your views, for example:
<%= "Email: {user.email}" if user.show_email? %> <%= "Address: {user.address}" if user.show_address? %> <%= "Age: {user.age}" if user.show_age? %>
Surya source share