如何在 Rails 中两次加入相同的 2 个模型?
我有一个带有国家/地区首选项的应用程序。用户将有两种类型的国家偏好 - 事件和研究。未来可能还会有更多。我更倾向于使用 2 个表来表示这一点,而不是使用 STI。我在优雅地配置 Rails 来执行此操作时遇到了一些麻烦。我可以破解它,但我宁愿按照 Rails 约定来做到这一点。我想要的是这样的:
class User < ActiveRecord::Base
has_many event_countries, :through => :event_countries, :class_name => 'Country'
has_many research_countries, :through => :research_countries, :class_name => 'Country'
end
class EventCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class ResearchCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class Country < ActiveRecord::Base
...
end
但这不起作用。鉴于这个“伪代码”,有人知道如何在 Rails 中实际实现它吗?
I have an app with country preferences. A user will have 2 types of country preferences - event and research. In the future there may be more. I was leaning more towards having 2 tables to represent this over using STI. I'm having a bit of trouble configuring Rails elegantly to do this. I could hack it but I would rather do this by Rails convention. What I want is something like this:
class User < ActiveRecord::Base
has_many event_countries, :through => :event_countries, :class_name => 'Country'
has_many research_countries, :through => :research_countries, :class_name => 'Country'
end
class EventCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class ResearchCountry < ActiveRecord::Base
belongs_to :country
belongs_to :user
end
class Country < ActiveRecord::Base
...
end
This doesn't work though. Given this "pseudo code" does anyone know how to actually implement this in Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你会宣布它们是错误的,因为这应该可以正常工作。这就是
:through
指令的用途:很多尴尬都来自于您为表格选择的标签。尽管它们乍一看似乎很合理,但使用它们的方式最终会让它们变得困难。
您可能需要调用类似于
user_research_countries
的research_countries
名称,以便关系名称可以为user.research_countries
作为:through
:您可以通过向用户-国家/地区关联表添加一个包含一个或多个标志的字段来进一步重构这一点,在本例中,该字段将是研究或事件或您稍后需要的任何内容:
I think you're going about declaring them wrong, because this should work properly. That's what the
:through
directive is for:A lot of the awkwardness comes from the labels you've chosen for the tables. Although they'd seem reasonable at first glance, the way you use them ends up making them difficult.
You might want to call
research_countries
something likeuser_research_countries
so that the relationship name can beuser.research_countries
as the:through
:You can refactor this even further by adding a field to the user-country association table that includes one or more flags, which in this case would be research or event or whatever you require later: