Ruby on Rails:两个模型都有/多个和Belongs_to

时间:2021-09-09 12:23:29

I've recently inherited an older rails application which has a model called Package. One of the tasks is to be able to allow Packages to be deleted. However, that's not currently possible, due to the way the PackageState and Package models are setup. Here's an example:

我最近继承了一个旧的rails应用程序,它有一个名为Package的模型。其中一项任务是允许删除包。但是,由于PackageState和Package模型的设置方式,目前无法实现。这是一个例子:

Package Model:

class Package < ActiveRecord::Base
  belongs_to :package_state, class_name: 'PackageState', foreign_key: :package_state_id
  has_many :package_states
end

And the PackageState:

和PackageState:

class PackageState < ActiveRecord::Base
  has_many :packages, :class_name => 'Package'
end

What would be the best way to go about reorganizing

什么是重组的最佳方式

1 个解决方案

#1


2  

What you're describing is a one-way has_and_belong_to_many (HABTM) association between Package and PackageState, which would look like this:

你所描述的是Package和PackageState之间的单向has_and_belong_to_many(HABTM)关联,如下所示:

class Package < ActiveRecord::Base
  has_and_belongs_to_many :package_states
end

class PackageState < ActiveRecord::Base
  has_many :packages
end

The accepted answer from this SO question has this excerpt about one-way HABTM associations:

这个SO问题的接受答案有关于单向HABTM关联的摘录:

Functionally yes, but semantically no. Using HABTM in a "one-sided" fashion will achieve exactly what you want. The name HABTM does unfortunately insinuate a reciprocal relationship that isn't always the case. Similarly, belongs_to :foo makes little intuitive sense here.

功能上是,但语义上没有。以“片面”方式使用HABTM将实现您想要的。不幸的是,HABTM的名称暗示了一种并非总是如此的互惠关系。同样,belongs_to:foo在这里没什么直观意义。

Don't get caught up in the semantics of HABTM and the other association, instead just consider where your IDs need to sit in order to query the data appropriately and efficiently. Remember, efficiency considerations should above all account for your productivity.

不要陷入HABTM和其他关联的语义,而只是考虑您的ID需要坐在哪里才能正确有效地查询数据。请记住,效率考虑首先应考虑到您的工作效率。

You also stated that

你也说过

that's not currently possible, due to the way the PackageState and Package models are setup

由于PackageState和Package模型的设置方式,目前无法实现

but that is just not correct. There is nothing stopping you from getting a package object using ActiveRecord and calling destroy on it.

但那是不正确的。没有什么能阻止你使用ActiveRecord获取包对象并在其上调用destroy。

You should ask yourself how you intend to use both models (particularly when it comes to deleting packages) and determine which association would work best. Considering this is an inherited application, you will likely need to deal with legacy data, but that's a topic for another day.

您应该问自己打算如何使用这两种模型(特别是在删除包时)并确定哪种关联最有效。考虑到这是一个继承的应用程序,您可能需要处理遗留数据,但这是另一天的主题。

#1


2  

What you're describing is a one-way has_and_belong_to_many (HABTM) association between Package and PackageState, which would look like this:

你所描述的是Package和PackageState之间的单向has_and_belong_to_many(HABTM)关联,如下所示:

class Package < ActiveRecord::Base
  has_and_belongs_to_many :package_states
end

class PackageState < ActiveRecord::Base
  has_many :packages
end

The accepted answer from this SO question has this excerpt about one-way HABTM associations:

这个SO问题的接受答案有关于单向HABTM关联的摘录:

Functionally yes, but semantically no. Using HABTM in a "one-sided" fashion will achieve exactly what you want. The name HABTM does unfortunately insinuate a reciprocal relationship that isn't always the case. Similarly, belongs_to :foo makes little intuitive sense here.

功能上是,但语义上没有。以“片面”方式使用HABTM将实现您想要的。不幸的是,HABTM的名称暗示了一种并非总是如此的互惠关系。同样,belongs_to:foo在这里没什么直观意义。

Don't get caught up in the semantics of HABTM and the other association, instead just consider where your IDs need to sit in order to query the data appropriately and efficiently. Remember, efficiency considerations should above all account for your productivity.

不要陷入HABTM和其他关联的语义,而只是考虑您的ID需要坐在哪里才能正确有效地查询数据。请记住,效率考虑首先应考虑到您的工作效率。

You also stated that

你也说过

that's not currently possible, due to the way the PackageState and Package models are setup

由于PackageState和Package模型的设置方式,目前无法实现

but that is just not correct. There is nothing stopping you from getting a package object using ActiveRecord and calling destroy on it.

但那是不正确的。没有什么能阻止你使用ActiveRecord获取包对象并在其上调用destroy。

You should ask yourself how you intend to use both models (particularly when it comes to deleting packages) and determine which association would work best. Considering this is an inherited application, you will likely need to deal with legacy data, but that's a topic for another day.

您应该问自己打算如何使用这两种模型(特别是在删除包时)并确定哪种关联最有效。考虑到这是一个继承的应用程序,您可能需要处理遗留数据,但这是另一天的主题。