has_many :through 关联的 Rails 复选框
一个人可以参加各种赛事,但他们必须输入该赛事的合作伙伴的名字。该关联存储在一个条目中,该条目包含一个合作伙伴姓名字段。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您仍在寻找答案,您可能需要查看我的 问题 和 答案< /a> 我发现的。
If you are still looking for the answer you might want to check out my question and answer that I found.
因此,您需要一个页面,您可以在其中为用户创建事件,并将人员添加到这些事件中,
我不会给您一个简单的解决方案,因为需要执行一些工作,但我建议您查看这些有关嵌套的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