程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Rails 结合了多个多态 has_many 关联大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Rails 结合了多个多态 has_many 关联?

开发过程中遇到Rails 结合了多个多态 has_many 关联的问题如何解决?下面主要结合日常开发的经验,给出你关于Rails 结合了多个多态 has_many 关联的解决方法建议,希望对你解决Rails 结合了多个多态 has_many 关联有所启发或帮助;

我有一个名为 Organization 的模型,它有很多 teams 和很多 collaborationsCollaboration 也有许多 teams。因此,团队模型与 OrganizationCollaboration 具有多态关联。

我想做的是 organization.teams 并反映组织中的所有团队以及协作中的所有团队都是组织的一部分。

表格

*organizations*
ID
user_iD
title
...

*collaborations*
ID
organization_ID
title
...

*teams*
ID
teamable_ID
teamable_type
title
...

模型

class Organization < ApplicationRecord

  has_many :orgaization_teams,as: :teamable,class_name: "Team"
  has_many :collaboration_teams,through: :collaborations,source: :teams,class_name: "Team"

end

class Collaboration < ApplicationRecord

  belongs_to :organization
  has_many :teams,as: :teamable

end

class Team < ApplicationRecord

  belongs_to :teamable,polymorphic: true
  belongs_to :organization,-> { joins(:teams).where(teams: {teamable_type: 'Organization'}) },foreign_key: 'teamable_ID'
  belongs_to :collaboration,-> { joins(:teams).where(teams: {teamable_type: 'Collaboration'}) },foreign_key: 'teamable_ID'

end

尝试

尝试 #1

class Organization < ApplicationRecord

  has_many :teams,-> { joins(:organization,:collaboration) },source: :teamable

end

结果: SystemStackerror(堆栈级别太深)

尝试#2

class Organization < ApplicationRecord

  def teams
    orgaization_teams.or(collaboration_teams)
  end

end

结果: ArgumentError(传递给 #or 的关系必须在结构上兼容。不兼容的值:[:joins])

潜在解决方案

我正在虑将 Team 上的多态关联分离为 {​​{1}} 和 organization_ID

所以新表看起来像这样

collaboration_ID

解决方法

我会用两个单独的外键代替:

 /index.html http/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET / http/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff http/1.1" 404 241 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff http/1.1" 404 239 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf http/1.1" 404 240 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf http/1.1" 404 238 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:53 -0400] "GET /first http/1.1" 404 203 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET /HNAP1/ http/1.1" 404 204 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET / http/1.1" 403 4897 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ http/1.1" 404 203 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ http/1.1" 404 203 "-" "-"

如果您想获得直接属于您想要的组织或其协作的团队:

#!/usr/bin/env python

import boto3


ec2 = boto3.resource('ec2')
ec2client = boto3.client('ec2')


#-----Define Lambda function-----#
def lambda_handler(event,context):

#-----check& filter Instances which  Instance State is running-----#
    instances = ec2client.describe_instances(
        Filters=[{
            'Name': 'instance-state-name','Values': ['pending','running']
        }]
        )

#-----Define Dictionary to store Tag Key & value------#
    Dict={}

    myTags = [{
        "Key" : "test_key","Value" : "test_value"
        }]

#-----Store Key & Value of Instance ------#
    for reservation in instances['Reservations']:
        for instance in reservation['Instances']:
            ec2.create_tags(
                resources = [instance["InstancEID"] ],Tags = myTags)
            for tag in instance['Tags']:
                if tag['Key'] == 'Name':
                    print ( instance['InstancEID'],tag['Value'])
                    #ids.append(( instance['InstancEID'],tag['Value']))
                    Dict[instance['InstancEID']]= tag['Value']
                
#-----Store Key & Value with attached instance ID of all volumes ------#     
    volumes = ec2.volumes.all() 
    for volume in volumes:

#-----compare Dictionary value Key:InstancEID and volume attached Key:InstancEID ------#     
        for a in volume.attachments:
            for key,value in Dict.items():


#-----Add tags to volumes ------#     

                if a['InstancEID'] == key:
                     volume.create_tags(tags =[{
                        'Key': 'test_key','Value': 'test_value'}
                    ])

我不认为这实际上可以写成关联,因为它在结构上过于复杂。

大佬总结

以上是大佬教程为你收集整理的Rails 结合了多个多态 has_many 关联全部内容,希望文章能够帮你解决Rails 结合了多个多态 has_many 关联所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。