如何在 Rails 中两次加入相同的 2 个模型?

发布于 2024-10-02 13:41:50 字数 680 浏览 2 评论 0原文

我有一个带有国家/地区首选项的应用程序。用户将有两种类型的国家偏好 - 事件和研究。未来可能还会有更多。我更倾向于使用 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 技术交流群。

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

发布评论

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

评论(1

我还不会笑 2024-10-09 13:41:50

我认为你会宣布它们是错误的,因为这应该可以正常工作。这就是 :through 指令的用途:

class User < ActiveRecord::Base
  has_many :event_countries
  has_many :countries_with_events,
    :through => :event_countries,
    :source => :country

  has_many :research_countries
  has_many :countries_with_researches,
    :through => :research_countries,
    :source => :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

很多尴尬都来自于您为表格选择的标签。尽管它们乍一看似乎很合理,但使用它们的方式最终会让它们变得困难。

您可能需要调用类似于 user_research_countriesresearch_countries 名称,以便关系名称可以为 user.research_countries 作为 :through

class User < ActiveRecord::Base
  has_many :user_event_countries
  has_many :event_countries,
    :through => :user_event_countries,
    :source => :country

  has_many :user_research_countries
  has_many :research_countries,
    :through => :user_research_countries,
    :source => :country
end

class UserEventCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class UserResearchCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class Country < ActiveRecord::Base
  # ...
end

您可以通过向用户-国家/地区关联表添加一个包含一个或多个标志的字段来进一步重构这一点,在本例中,该字段将是研究或事件或您稍后需要的任何内容:

class User < ActiveRecord::Base
  has_many :user_countries
  has_many :event_countries,
    :through => :user_countries,
    :source => :country,
    :conditions => { :event => true }
  has_many :research_countries,
    :through => :user_countries,
    :source => :country,
    :conditions => { :research => true }
end

class UserCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user

  # * column :event, :boolean
  # * column :research, :boolean
end

class Country < ActiveRecord::Base
  # ...
end

I think you're going about declaring them wrong, because this should work properly. That's what the :through directive is for:

class User < ActiveRecord::Base
  has_many :event_countries
  has_many :countries_with_events,
    :through => :event_countries,
    :source => :country

  has_many :research_countries
  has_many :countries_with_researches,
    :through => :research_countries,
    :source => :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

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 like user_research_countries so that the relationship name can be user.research_countries as the :through:

class User < ActiveRecord::Base
  has_many :user_event_countries
  has_many :event_countries,
    :through => :user_event_countries,
    :source => :country

  has_many :user_research_countries
  has_many :research_countries,
    :through => :user_research_countries,
    :source => :country
end

class UserEventCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class UserResearchCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class Country < ActiveRecord::Base
  # ...
end

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:

class User < ActiveRecord::Base
  has_many :user_countries
  has_many :event_countries,
    :through => :user_countries,
    :source => :country,
    :conditions => { :event => true }
  has_many :research_countries,
    :through => :user_countries,
    :source => :country,
    :conditions => { :research => true }
end

class UserCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user

  # * column :event, :boolean
  # * column :research, :boolean
end

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