I want to implement a feature where user can "add additional details" 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.
如何使用具有关联User has_one Profile的新模型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).
如果我只有标签和文本的新细节,我可以尝试哈希,但因为我还必须从用户获取用户是否希望隐藏或可见细节的详细信息,我可能需要额外的用于存储该值的字段(true或false)。
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.
我正在研究Rails 3.2。
2 个解决方案
#1
2
I don't really see your problem (or I don't get it), but:
我真的没有看到你的问题(或者我没有得到它),但是:
Why don't you just create a has_many AdditionalData
with user_id:integer name:string content:string visible:boolean
?
为什么不用user_id创建一个has_many AdditionalData:integer name:string content:string visible:boolean?
So you can loop through @user.additional_datas.visible
(assuming you defined a scope scope :visible, where("visible = 1")
.
因此,您可以遍历@ user.additional_datas.visible(假设您定义了范围范围:visible,where(“visible = 1”)。
Added benefit: You could make this model polymorph and add additional information to other things you have in your app without the need to create an extra "information" table for every model you want to store these.
增加的好处:您可以使此模型变为多态并将其他信息添加到应用程序中的其他内容,而无需为要存储这些内容的每个模型创建额外的“信息”表。
For validation, you could also add a data_type
field and create validations according to the data type (needs to be an url, a phone number, just text, ...)
对于验证,您还可以添加data_type字段并根据数据类型创建验证(需要是网址,电话号码,只是文本,...)
#2
1
I don't see the problem too; I think an hash-like table could be the solution, something like this:
我也没有看到问题;我认为类似哈希的表可能是解决方案,如下所示:
rails generate model UserDetails label:string value:string \
visible:boolean user:references
in which you put records like this:
你在其中放置这样的记录:
user.details.create(:label => 'Dog name', :value => 'Fuffy' :visible => false)
#1
2
I don't really see your problem (or I don't get it), but:
我真的没有看到你的问题(或者我没有得到它),但是:
Why don't you just create a has_many AdditionalData
with user_id:integer name:string content:string visible:boolean
?
为什么不用user_id创建一个has_many AdditionalData:integer name:string content:string visible:boolean?
So you can loop through @user.additional_datas.visible
(assuming you defined a scope scope :visible, where("visible = 1")
.
因此,您可以遍历@ user.additional_datas.visible(假设您定义了范围范围:visible,where(“visible = 1”)。
Added benefit: You could make this model polymorph and add additional information to other things you have in your app without the need to create an extra "information" table for every model you want to store these.
增加的好处:您可以使此模型变为多态并将其他信息添加到应用程序中的其他内容,而无需为要存储这些内容的每个模型创建额外的“信息”表。
For validation, you could also add a data_type
field and create validations according to the data type (needs to be an url, a phone number, just text, ...)
对于验证,您还可以添加data_type字段并根据数据类型创建验证(需要是网址,电话号码,只是文本,...)
#2
1
I don't see the problem too; I think an hash-like table could be the solution, something like this:
我也没有看到问题;我认为类似哈希的表可能是解决方案,如下所示:
rails generate model UserDetails label:string value:string \
visible:boolean user:references
in which you put records like this:
你在其中放置这样的记录:
user.details.create(:label => 'Dog name', :value => 'Fuffy' :visible => false)