I want to implement a feature where user can “add new attributes” to his profile. Here he should be able to create the label for the detail and the actual details like:
Education : Degree
where Education is the label for the detail and Degree is the detail.
Apart from this, he should also have an option to decide whether this details should be made visible or hidden.
How can I implement this using a new model Profile with the association
User has_one Profile.
If I just had Label and Text for the new details, I could have tried hash, but since I would also have to get the details from the user on whether the user wants the detail to be made hidden or visible, I might require an extra field to store that value (true or false).So how can I implement that ?
I cannot create a has_many Profile user_id:integer name:string content:string visible:boolean since what I need is a has_one association.
I am really confused as to how I can get the whole thing implemented together.
Please suggest me how I can implement this and also how can I update the model each time a user creates a new detail without changing the schema of the db.
I am working on Rails 3.2.
Assuming you have a column named
attributesof type text in your profile tableThen you can use the serialize method in your model:
This will allow you to write code like this:
The Hash will be serialized in YAML format.
Edit: CRUD operations
To add or modify the education:
To query for all the visible attributes:
To delete the education: