将 has_many :through 投入使用并以实际形式使用它

发布于 2024-11-07 15:52:26 字数 843 浏览 5 评论 0 原文

class Allowedevent < ActiveRecord::Base
    :belongs_to :room
    :belongs_to :event
end

class Room < ActiveRecord::Base
    :has_many :allowedevents
    :has_many :events => :through => allowedevents
end

class Event< ActiveRecord::Base
    :has_many :allowedevents
    :has_many :rooms=> :through => allowedevents
end

我很难将上述关系放入表单中或在控制台中使用它们。

问题:

  • 现在假设我保存了一个房间,我是否必须将 ID 显式添加到 allowedevents 表中? 我必须这样做吗?

    房间=房间.new; room.title = "测试"; room.allowedevents = ""...?

    正如您从上面看到的,我不知道如何保存实际记录。

  • 基本上我想问如何使用上述关系将一个房间保存到具有许多allowedevents的数据库中。我是否必须循环访问用户输入并将每个输入保存到 allowedevents 中?有更好的方法吗?

  • 我从 Railscasts 剧集中获得了以上内容,是否有关于 Railscasts 的剧集实际上从角度来阐述如何在前端使用它?

class Allowedevent < ActiveRecord::Base
    :belongs_to :room
    :belongs_to :event
end

class Room < ActiveRecord::Base
    :has_many :allowedevents
    :has_many :events => :through => allowedevents
end

class Event< ActiveRecord::Base
    :has_many :allowedevents
    :has_many :rooms=> :through => allowedevents
end

I'm having difficulty putting the above relationships into a form or playing around with them in the console.

Questions:

  • Now say I save a room, do I have to explicitly add IDs into the allowedevents table?
    do I have to do this?

    room = Room.new; room.title = "test"; room.allowedevents = ""...?

    as you can see from above, I am lost as to how to save the actual record.

  • basically I want to ask how to save a room into the database that has many allowedevents using the above relationships. Do I have to loop through the user input and save each one to allowedevents? is there a better way?

  • I got the above from railscasts episode, is there an episode on railscasts that actually puts this in perspective on how to use it in the front end?

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

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

发布评论

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

评论(1

就像说晚安 2024-11-14 15:52:26

前端可以是房间的编辑页面,其中将所有事件列为一组复选框。然后,您可以检查允许预订该房间的活动。

在房间模型中处理这个问题有点棘手。有些人会建议使用accepts_nested_attributes_for,但是当用户稍后取消选中这些框时,它不会自动删除该关系。

Accepts_nested_attributes_for 方法有一个删除记录的选项,但强制您为要处理的每个记录传递单独的“_delete”参数。如果您想在有人取消选中该框后使用 javascript 将虚拟“_delete”参数添加到表单中,那么这一切都很好,但如果您不想依赖 javascript,那就会很棘手。

因此,我决定放弃accepts_nested_attributes_for并推出我自己的解决方案,可能类似于Ryan Bates在accepts_nested_attributes_for存在之前解决这个问题的方式。

这里没有发布我的解决方案,而是旧的 RailsCast 剧集的链接,它解释了如何处理复杂表单内的嵌套模型:

http://railscasts.com/episodes/73-complex-forms-part-1

如果其他人有使用accepts_nested_attributes_for的新颖方法在 has_many 中带有复选框:通过风格关系,我很想听听。

The front end could be an edit page for room that lists all the events as a set of check boxes. You could then check off the events this room is allowed be reserved for.

Handling this in the room model is a little trickier. Some people would recommend using accepts_nested_attributes_for, but when users later un-check the boxes it doesn't automatically delete the relationship.

The accepts_nested_attributes_for method has an option for deleting records, but forces you to pass in a separate "_delete" parameter for each record you want to dispose of. This is all good if you want to use javascript to add that virtual "_delete" parameter to the form after someone un-checks the box, but if you don't want to rely on javascript it gets tricky.

Therefore I've made the decision to forgo accepts_nested_attributes_for and just roll my own solution, probably similar to how Ryan Bates solved this problem before accepts_nested_attributes_for existed.

Rather than posting my solution, here's a link to the old RailsCast episode which explains how to handle nested models inside a complex form:

http://railscasts.com/episodes/73-complex-forms-part-1

If anyone else has a novel approach to using accepts_nested_attributes_for with checkboxes in a has_many :through style relationship, I would love to hear it.

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