Rails - 对象的类型应该在它自己的模型中吗?

发布于 2024-08-19 22:34:25 字数 623 浏览 4 评论 0原文

我有一个联系人,并且联系 has_many :phones。电话表有一列名为phones_desc,我想在其中包含用户保存的电话号码的类型。

我的问题/最佳实践

我应该提供一个选择,其中包含手动提供的选项(例如“移动”、“工作”、“家庭”)...

-或者-

...创建一个名为的新模型phones_types,我可以在其中添加我想要的值,从而为每个电话创建一个唯一的 ID。然后,在将phone_desc更改为phone_type_id,添加phones has_many:phone_types并为phone_types表提供名称列后,我可以执行以下操作:

@phone = Phone.first
@phone.type.name

Sidenote

我目前正在使用第一个选项(不是单独的模型)当我去编辑父对象时,我在选择值时遇到了问题。换句话说,在编辑电话号码时,选择选项没有选择已保存的值。

它始终选择第一个选项,因此用户可能会无意中更改phone_desc而没有意识到。

如果第一个选项实际上是更好的方法,那么您是否了解在通过选择编辑手机描述时如何使对象值成为所选值?

I have a contact, and contact has_many :phones. The phones table has a column named, phones_desc, where I want to include the type of phone number the user has saved.

My question / Best practice

Should I provide a select with manually provided options (such as "mobile", "work", "home")...

-or-

...create a new model named phones_types where I can add the values I'd like, thus creating a unique ID for each one. Then I can do the following after changing phone_desc to phone_type_id, adding phones has_many :phone_types and giving the phone_types table a name column:

@phone = Phone.first
@phone.type.name

Sidenote

I'm currently doing it with the first option (not a separate model) and am having trouble with it selecting the value when I go to edit the parent object. In other words, the select options don't have the saved value selected when going to edit phone numbers.

It always has the first option selected, so the user may inadvertently change the phone_desc without realizing it.

If the first option is in fact the better way to go, would you have any insight on how to get the objects value to be the selected value when editing phones descriptions via a select?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-08-26 22:34:25

我仍然会选择针对手机类型的单独型号。这样,您以后就可以更轻松地添加其他电话类型。还要考虑一下 i18n,它可能会帮你在将“Mobile”翻译成日语时省去一些麻烦。

I'd still go for a separate model for the phone type. This way, you can more easily add other phone types later on. Also think about i18n, it might save you some headache when translating "Mobile" to Japanese.

爱给你人给你 2024-08-26 22:34:25

事实证明,选项一工作得很好,而且我也可以让所选选项工作。这是我假设选项参数的顺序的问题。

我将: 更改

<%= f.select :number_desc, '<option value="mobile">Mobile</option><option value="work">Work</option><option value="home">Home</option><option value="other">Other</option' %>

为:

<%= f.select :number_desc, [["Mobile", "mobile"], ["Work", "work"], ["Home", "home"], ["Other", "other"]] %>

瞧 - 选择的作品很好。 :)

Turns out that option one works just fine, and I can get the selected option to work too. It was a problem in the order that I supposed the option parameters.

I changed:

<%= f.select :number_desc, '<option value="mobile">Mobile</option><option value="work">Work</option><option value="home">Home</option><option value="other">Other</option' %>

to:

<%= f.select :number_desc, [["Mobile", "mobile"], ["Work", "work"], ["Home", "home"], ["Other", "other"]] %>

And voila - selected works fine. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文