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?
发布评论
评论(1)
前端可以是房间的编辑页面,其中将所有事件列为一组复选框。然后,您可以检查允许预订该房间的活动。
在房间模型中处理这个问题有点棘手。有些人会建议使用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.