如果一个对象有多个belongs_to,我该如何创建它?
我有以下内容:
class Org < ActiveRecord::Base
has_many :users
has_many :entries
end
class Entry < ActiveRecord::Base
belongs_to :org
belongs_to :user
validates_presence_of :entry_text
end
class User < ActiveRecord::Base
belongs_to :org
has_many :entries
validates_uniqueness_of :user_name
validates_presence_of :user_name, :length => { :minimum => 3 }
end
我可以创建组织和用户...如果有两个belongs_to,我如何创建一个条目?这种模式叫什么?
I have the following:
class Org < ActiveRecord::Base
has_many :users
has_many :entries
end
class Entry < ActiveRecord::Base
belongs_to :org
belongs_to :user
validates_presence_of :entry_text
end
class User < ActiveRecord::Base
belongs_to :org
has_many :entries
validates_uniqueness_of :user_name
validates_presence_of :user_name, :length => { :minimum => 3 }
end
I can Create Orgs and Users... How do i create an entry if there are two belongs_to? and what is this pattern called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
双重嵌套资源很棘手。用户的技巧通常是将其排除在您想要的进入路径之外。
您的问题有点宽泛,但如果您指定更多信息,人们将能够更好地帮助您。另外,我建议您使用 gem Devise 作为您的用户管理系统。由于您使用的是“用户”,我假设您希望组织中的用户创建条目。创建的条目将是组织的一部分,用户将是会话的当前用户。抱歉,如果我的假设是错误的。
你的routes.rb文件看起来像这样(假设rails 3):
resources :orgs do
资源:条目
然后
你的入口控制器的创建看起来像:
你想要通过创建一个加载当前组织并执行 before_filter :loadOrg 的方法来为整个类设置组织
这当然假设你的路径是这样的: /org/(id)/entry/(entry_id)
而不是
/org/(id)/user/(user_id)/entry/(entry_id) ,
在我看来这是不必要的,并且可能会导致更多问题。您始终可以创建一个用户页面模型来调用用户的所有条目,但默认路由不一定必须在路径中包含用户。
Double nested resources are tricky. The trick with users usually is to keep it out of your desired entry path.
Your question is kind of broad, but if you specify more information, people would be able to help you better. Also, I would recommend using the gem Devise for your user management system. Since you're using 'users' I would assume you want users from orgs to create entries. The entry created would be a part of org and the user would be the session's current user. Sorry if I am wrong to assume this.
Your routes.rb file can look something like this (assuming rails 3):
resources :orgs do
resources :entries
end
Then the create of your entry controller would look like:
And you'd want to set the org for the entire class by making a method that loads your current org and do a before_filter :loadOrg
This is of course assuming your path is something like: /org/(id)/entry/(entry_id)
and not
/org/(id)/user/(user_id)/entry/(entry_id)
which in my opinion is unnecessary and can lead to more problems. You can always create a userpage model that calls all entries by users, but the default route doesn't necessarily have to include users in the path.
我没有看到任何问题。
现在问题是澄清您需要什么:
@entry
可以同时属于org
和user
吗?或者它只能属于其中之一?@entry
应该至少属于其中之一吗?如果
@entry
应该只属于其中之一,那么你应该使用多态性
I don't see any problem.
Now questions to clarify what do you need:
@entry
belongs bothorg
anduser
at the same time? Or it can belongs to only one of them?@entry
belongs to at least one of them?If
@entry
supposed to belong only one of them, so you should usePolymorphism