has_many :through 关联的 Rails 复选框

发布于 2024-12-23 15:43:11 字数 659 浏览 0 评论 0原文

一个人可以参加各种赛事,但他们必须输入该赛事的合作伙伴的名字。该关联存储在一个条目中,该条目包含一个合作伙伴姓名字段。

class Person < ActiveRecord::Base
    has_many :entries
    has_many :events, :through => :entries

    validates_presence_of :name
end

class Event < ActiveRecord::Base
end

class Entry < ActiveRecord::Base
    belongs_to :person
    belongs_to :event

    validates_presence_of :partner_name
end

问题是:如何创建一个单页表单,允许一个人在多个事件中输入自己并输入其合作伙伴的姓名?我尝试在人员模型中实现一个 all_entries 方法,该方法将返回所有可用事件的条目对象数组,以及一个 all_entries_attributes 方法,该方法将更新、创建,并删除条目对象,但我似乎找不到一个好的、干净的方法来做到这一点。我知道这是一个相当开放式的问题,但这一定是 Rails 社区中其他人以前遇到过的模式,所以我希望有一个好的解决方案。

A person can compete in various events, but they must enter a partner's name for that event. This association is stored in an entry, which contains a field for the partner's name.

class Person < ActiveRecord::Base
    has_many :entries
    has_many :events, :through => :entries

    validates_presence_of :name
end

class Event < ActiveRecord::Base
end

class Entry < ActiveRecord::Base
    belongs_to :person
    belongs_to :event

    validates_presence_of :partner_name
end

The question is: How do you create a single page form that allows a person to enter themselves in multiple events and input their partners' names? I've tried to implement an all_entries method in the person model that will return an array of entry objects for all the available events, and an all_entries_attributes method that will update, create, and delete entry objects, but I can't seem to find a good, clean way to do this. I know this is a rather open ended question, but this must be a pattern that someone else in the rails community has encountered before, so I'm hoping there is a good solution to it.

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

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

发布评论

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

评论(2

原来分手还会想你 2024-12-30 15:43:11

如果您仍在寻找答案,您可能需要查看我的 问题答案< /a> 我发现的。

If you are still looking for the answer you might want to check out my question and answer that I found.

清风夜微凉 2024-12-30 15:43:11

因此,您需要一个页面,您可以在其中为用户创建事件,并将人员添加到这些事件中,

我不会给您一个简单的解决方案,因为需要执行一些工作,但我建议您查看这些有关嵌套的railscast模型表单

part1第 2 部分

So you want a page in which you can create events for a user, and add people to those events

I won't give you a plain solution because there's some work to an implementation for this, but I will recommend checking out these railscasts about nested model forms

part1 and part2

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