简体   繁体   English

Rails数据库关联多态关联

[英]Rails Database Associations Polymorphic Association

I've read a few of the help topics and the ruby documentation on PolyMorphic Associations and am still not sure if that is the way to go. 我已经阅读了一些有关PolyMorphic Associations的帮助主题和ruby文档,但我仍然不确定这是否可行。 After struggling with the concept and trying to figure this out, I thought of reaching out here. 在与这个概念挣扎并试图解决这个问题后,我想到了这里。

What I tried so far was this, which breaks all other CRUD operations from happening in the libraries and departments models. 到目前为止我尝试过的是打破图书馆和部门模型中所有其他CRUD操作。

Normal Hours Class 正常时间班

class NormalHour < ApplicationRecord
  belongs_to :resource, polymorphic: true
end

Library Class 图书馆班

class Library < ApplicationRecord
  has_many :departments, dependent: :destroy
  has_many :normal_hours, as: :resource, dependent: :destroy

  def departments
      departments = Department.where(:library_id => self.id)
  end
end

Departments Class 部门类

class Department < ApplicationRecord
  belongs_to :library, inverse_of: :departments
  has_many :normal_hours, as: :resource, dependent: :destroy
end

The problem becomes that after adding those I can no longer add or delete anything from the libraries or departments. 问题变成了在添加后我无法再添加或删除库或部门中的任何内容。

Any thoughts? 有什么想法吗? Tutorials? 教程? Samples? 样品? Etc? 等等?

在此输入图像描述

UPDATE UPDATE

+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| resource_type | varchar(255) | YES  | MUL | NULL    |                |
| resource_id   | int(11)      | YES  |     | NULL    |                |
| day_of_week   | int(11)      | YES  |     | NULL    |                |
| open          | time         | YES  |     | NULL    |                |
| close         | time         | YES  |     | NULL    |                |
| created_at    | datetime     | NO   |     | NULL    |                |
| updated_at    | datetime     | NO   |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+

UPDATE 2 更新2

It appears that I'm having issues with the views now, but simplifying the view and modifying my Library and Department classes has fixed my original problem. 看来我现在遇到了一些问题,但是简化视图和修改我的图书馆和部门类已经解决了我原来的问题。 Now I'm trying to figure out the best case scenario for making a dynamic form that changes the type based on selected object and loads both libraries and departments into a select field. 现在,我正在尝试找出制作动态表单的最佳案例场景,该表单根据所选对象更改类型,并将库和部门加载到选择字段中。

removed dependent: :destroy 删除 dependent: :destroy

Library Class 图书馆班

class Library < ApplicationRecord
  has_many :departments, dependent: :destroy
  has_many :normal_hours, as: :resource

  def departments
      departments = Department.where(:library_id => self.id)
  end
end

Departments Class 部门类

class Department < ApplicationRecord
  belongs_to :library, inverse_of: :departments
  has_many :normal_hours, as: :resource
end

UPDATE 3 更新3

Problem Update: 问题更新:

After Update 2 I think my true problem is figuring out the views for this stuff. 在更新2之后,我认为我的真正问题在于找出这些内容的观点。 I know that the department and library classes will have access to normal_hours information. 我知道部门和图书馆课程可以访问normal_hours信息。 The problem that I'm having is in entering the data through forms and using that same form to update that data. 我遇到的问题是通过表单输入数据并使用相同的表单来更新数据。

  <div class="field" >
    <%= f.collection_select(:resource_id, Library.all, :id, :name)  %>
  </div>

  <div class="field">
    <%= f.collection_select(:resource_id, Department.all, :id, :reformat_name) %>
  </div>

I need a condition to turn off the fields or hide the fields based on the type of information being entered. 我需要一个条件来关闭字段或根据输入的信息类型隐藏字段。 I thought about putting a check button or question that uses JS to hide/show the correct drop down, but what kind of a logic do I need to change the specific class based on the type or is there a better way. 我考虑过使用JS来隐藏/显示正确的下拉列表的检查按钮或问题,但是我需要根据类型更改特定类需要什么样的逻辑,或者有更好的方法。

I was thinking that potentiall in the collection_select I could use the reference type to call the class @normal_hours.resource_type.all but I doubt that would instantiate a new class. 我在@normal_hours.resource_type.all认为我可以使用引用类型来调用类@normal_hours.resource_type.all但我怀疑它会实例化一个新类。

I think you've got the schema wrong for polymorphic associations. 我认为你有多态关联的架构错误。 Take a look at the rails guide on polymorphic associations . 看看有关多态关联rails指南 Your normal_hours table should have two fields: resource_id and resource_type. 您的normal_hours表应该有两个字段:resource_id和resource_type。 Using those two keys, a NormalHour can belong to either a Library or a Department. 使用这两个键,NormalHour可以属于库或部门。 In one case, the values in those fields would be "5" and "Library" and the other case "12" and "Department". 在一种情况下,这些字段中的值将是“5”和“库”,而另一种情况是“12”和“部门”。 The schema in your picture is not polymorphic - its simply that NormalHour can belong to either of those. 图片中的模式不是多态的 - 简单地说,NormalHour可以属于其中任何一个。

This could could be causing problems as well: 这可能也会导致问题:

def departments
  departments = Department.where(:library_id => self.id)
 end

That is overriding the definition of departments with the has_many association. 这就是用has_many关联覆盖部门的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM