Rails:这些模型的正确关联是什么?

发布于 2024-11-17 05:34:07 字数 852 浏览 8 评论 0原文

这个问题的正确关联是什么?

共有三种模型:

  • 居民
  • 地址

每个居民都有一个地址

每个地址可以属于一个< code>Resident 或 Party 或两者。

同一地址可以有多个参与方,并且该位置可以有多个居民居住。

habtm 关系是这些模型的最佳解决方案吗?

我考虑过进行多态关联,但出现了冲突,因为地址可能同时属于ResidentParty多个有时


我希望能够做一些事情,例如...

address = Address.find_or_create_by_street("100 Some Street")

# Associate the Party with a specific Address:
party_object.address = address

# Find all Parties happening at a specific Address:
address.parties do ...

# Find all Residents located at a specific Address:
address.residents.each do ...

运行 Rails 3 + MySQL 5.5

What is the correct association for this problem?

There are three models:

  • Residents
  • Parties
  • Addresses

Each Resident and Party has an Address

Each Address can belong to a Resident or Party or to both.

There can be multiple Parties at the same Address and multiple Residents living at that location.

Is a habtm relationship the best solution for these models?

I looked into doing polymorphic associations but a conflict arises because an Address might belong to both Resident and to Party mulitple times


I would like to be able to do things like...

address = Address.find_or_create_by_street("100 Some Street")

# Associate the Party with a specific Address:
party_object.address = address

# Find all Parties happening at a specific Address:
address.parties do ...

# Find all Residents located at a specific Address:
address.residents.each do ...

Running Rails 3 + MySQL 5.5

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

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

发布评论

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

评论(2

酒儿 2024-11-24 05:34:07

我认为不需要多态性

class Address < ActiveRecord::Base
  has_many :parties
  has_many :residents
end

class Party < ActiveRecord::Base
  belongs_to :address
end

class Resident < ActiveRecord::Base
  belongs_to :address
end

 # Address can have multiple parties, and multiple residents. 
 # Resident belongs to an address (lives at only 1 address). 
 # A party belongs to an address (party can be only at 1 address).

address = Address.create(:name => "10 something street.")
address.parties << Party.create(:name => "first")
address.parties << Party.create(:name => "second")
address.residents << Resident.create(:name => "John")
address.residents << Resident.create(:name => "Jane")


Party.first.address.name #10 something street.
Resident.first.address.name #10 something street.
address.parties.size #2
address.residents.size #2

Resident.first.address.parties.size #2

I don't see a need for Polymorphism

class Address < ActiveRecord::Base
  has_many :parties
  has_many :residents
end

class Party < ActiveRecord::Base
  belongs_to :address
end

class Resident < ActiveRecord::Base
  belongs_to :address
end

 # Address can have multiple parties, and multiple residents. 
 # Resident belongs to an address (lives at only 1 address). 
 # A party belongs to an address (party can be only at 1 address).

address = Address.create(:name => "10 something street.")
address.parties << Party.create(:name => "first")
address.parties << Party.create(:name => "second")
address.residents << Resident.create(:name => "John")
address.residents << Resident.create(:name => "Jane")


Party.first.address.name #10 something street.
Resident.first.address.name #10 something street.
address.parties.size #2
address.residents.size #2

Resident.first.address.parties.size #2
友欢 2024-11-24 05:34:07

Rails 专家似乎正在逐步停止使用 habtm。我认为你可以用一个简单的 has_many 来做到这一点:

class Resident < ActiveRecord::Base
  belongs_to :address
end

class Party < ActiveRecord::Base
  belongs_to :address
end

class Address < ActiveRecord::Base
  has_many :residents 
  has_many :parties
end

从这些关联中,你可以做类似

party.address
address.residents
address.parties
party.address.residents
resident.address.parties

Schema

的事情SQL 模式(省略 Rails 默认值)将与此一起使用,看起来像:

create_table "residents" do |t|
  t.integer  "address_id"
  t.string   "name"
  ... etc
end

create_table "parties" do |t|
  t.integer  "address_id"
  t.datetime "start_time"
  ... etc.
end

create_table "addresses" do |t|
  t.string   "first_line"
  ...etc.
end

编辑
更新:has_one -> :belongs_to 按照 @klochner 的正确建议,关于居民和当事人。

The Rails gurus seem to be phasing out their use of habtm. I think you can do it with a straightforward has_many:

class Resident < ActiveRecord::Base
  belongs_to :address
end

class Party < ActiveRecord::Base
  belongs_to :address
end

class Address < ActiveRecord::Base
  has_many :residents 
  has_many :parties
end

From those associations you can do things like

party.address
address.residents
address.parties
party.address.residents
resident.address.parties

Schema

The SQL schema (leaving out the Rails defaults) that would go with this would look something like:

create_table "residents" do |t|
  t.integer  "address_id"
  t.string   "name"
  ... etc
end

create_table "parties" do |t|
  t.integer  "address_id"
  t.datetime "start_time"
  ... etc.
end

create_table "addresses" do |t|
  t.string   "first_line"
  ...etc.
end

EDIT
Updated :has_one -> :belongs_to on Residents and Parties as correctly advised by @klochner.

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