Rails 建模 - 让用户在相同形式的两个相关模型 (sti) 之间进行选择

发布于 2024-11-28 01:59:29 字数 852 浏览 1 评论 0原文

以下是我的模型:

class BillingProfile < ActiveRecord::Base
  belongs_to :student
  attr_accessible :cost
end

class PerEventBillingProfile < BillingProfile
  belongs_to :event_category
end

class FlatFeeBillingProfile < BillingProfile
  attr_accessible :interval, :frequency
end

学生可以拥有许多这两种类型的计费配置文件。我想要的是我的学生创建表单中的一个单选按钮,让用户可以在创建 PerEventBillingProfile 和 FlatFeeBillingProfile 之间进行选择。当选择每个事件单选时,将显示 PerEventBillingProfile 的字段,反之亦然。为了在这个模型设置中实现这一点,看来我必须这样做:

class Student < ActiveRecord::Base
  has_many :per_event_billing_profiles
  has_many :flat_fee_billing_profiles
  accepts_nested_attributes_for :per_event_billing_profiles
  accepts_nested_attributes_for :flat_fee_billing_profiles
end

感觉这可以更简单。有没有更直接的方法来获得我想要的东西?我意识到我可以将所有这些内容填充到一个模型中,并且在我的列中只包含一堆 NULL 值,但我也不喜欢那样。

Here are my models:

class BillingProfile < ActiveRecord::Base
  belongs_to :student
  attr_accessible :cost
end

class PerEventBillingProfile < BillingProfile
  belongs_to :event_category
end

class FlatFeeBillingProfile < BillingProfile
  attr_accessible :interval, :frequency
end

Students can have many billing profiles of both types. What I'd like is a radio button in my student creation form that lets the user choose between creating a PerEventBillingProfile and a FlatFeeBillingProfile. When the per event radio is chosen, fields for a PerEventBillingProfile would show up, and vice versa. In order to get that to happen with this model setup, it appears I'd have to do:

class Student < ActiveRecord::Base
  has_many :per_event_billing_profiles
  has_many :flat_fee_billing_profiles
  accepts_nested_attributes_for :per_event_billing_profiles
  accepts_nested_attributes_for :flat_fee_billing_profiles
end

It feels like this could be simpler. Is there a more straightforward way to get what I want? I realize that I could stuff all of this into one model and just have a bunch of NULL values in my columns, but I don't like that either.

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

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

发布评论

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

评论(1

So尛奶瓶 2024-12-05 01:59:29

这就是我是如何度过这个难关的。我将 has_many :billing_profiles 行保留在 Student 中。在表单中,我这样做了:

<%= f.fields_for :billing_profiles do |builder| %>
  <tr>
    <%= render 'billing_profile_fields', :f => builder %>
  <tr>
<% end %>

在部分中:

<td>
  <%= f.label :type, "Profile type" %>
  <%= f.select :type, { "Per Event" => "PerEventBillingProfile", "Flat Fee" => "FlatFeeBillingProfile" } %>
</td>

我使用 JS 隐藏了与当前选择的类型无关的字段。但这确实意味着所有验证都必须进入 BillingProfile,这有点违背了 sti 的目的。

Here's how I got through this. I kept the has_many :billing_profiles line in Student. In the form I did this:

<%= f.fields_for :billing_profiles do |builder| %>
  <tr>
    <%= render 'billing_profile_fields', :f => builder %>
  <tr>
<% end %>

And in the partial:

<td>
  <%= f.label :type, "Profile type" %>
  <%= f.select :type, { "Per Event" => "PerEventBillingProfile", "Flat Fee" => "FlatFeeBillingProfile" } %>
</td>

And I hide the fields which are irrelevant to the currently selected type using JS. This does mean that all the validations have to go in BillingProfile however, which kinda defeats the purpose of sti.

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