标签: polymorphic-associations

rails多态关联(遗留数据库)

我正在使用遗留数据库,因此我无法控制数据模型.他们使用了很多多态链接/连接表,就像这样

create table person(per_ident, name, ...)

create table person_links(per_ident, obj_name, obj_r_ident)

create table report(rep_ident, name, ...)
Run Code Online (Sandbox Code Playgroud)

obj_nametable-name 在哪里,obj_r_ident是标识符.所以链接的报告将插入如下:

insert into person(1, ...)
insert into report(1, ...)
insert into report(2, ...)

insert into person_links(1, 'REPORT', 1)
insert into person_links(1, 'REPORT', 2)
Run Code Online (Sandbox Code Playgroud)

然后,人1将有2个链接报告,1和2.

我可以理解像这样的数据模型可能带来的好处,但我主要看到一个很大的缺点:使用约束不可能确保数据完整性.但是,唉,我不能再改变它了.

但是要在Rails中使用它,我正在寻找多态关联,但没有找到一个很好的方法来解决这个问题(因为我不能改变列名,并且没有找到办法做到这一点).

我确实提出了一个解决方案.请提供建议.

class Person < ActiveRecord::Base

  set_primary_key "per_ident"
  set_table_name "person"
  has_and_belongs_to_many :reports,
                         :join_table => "person_links",
                         :foreign_key => "per_ident",
                         :association_foreign_key => "obj_r_ident",
                         :conditions => "OBJ_NAME='REPORT'"
end

class Report < ActiveRecord::Base

  set_primary_key "rep_ident"
  set_table_name "report"
  has_and_belongs_to_many :persons, …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails polymorphic-associations

5
推荐指数
2
解决办法
3788
查看次数

Rails - 简洁地找到多态嵌套资源的父资源

假设我有这样的多态结构.

map.resources :bar, :has_many => :foo
map.resources :baz, :has_many => :foo
map.resources :qux, :has_many => :foo

class Foo
  belongs_to :parent, :polymorphic => true
end

class FooController < AC
 before_filter :find_parent

 ...

 private
 def find_parent
  # ugly way:
  @parent = if params[:bar_id]
   Bar.find(params[:bar_id])
  elsif params[:baz_id]
   Baz.find(params[:baz_id])
  elsif params[:qux_id]
   Qux.find(params[:qux_id])
  end 
 end
end
Run Code Online (Sandbox Code Playgroud)

那太难看了.每当我们添加一个它可能属于的新东西时,我们需要将它非干扰地添加到before_filter.

它也变得更糟.假设Foos 真的是可以在任何地方出现的多态事物,比如评论或标签.并假设您有以下路线:

map.resources :bar, :has_many => :foo do |bar|
 bar.resources :baz, :has_many => :foo
end
map.resources :qux, :has_many => :foo do |qux|
 qux.resources :baz, :has_many => :foo …
Run Code Online (Sandbox Code Playgroud)

routing routes ruby-on-rails polymorphic-associations

5
推荐指数
1
解决办法
1327
查看次数

jpa连接查询子类

我在JPA(hibernate)中有以下关系.

对象X有两个子类,Y和Z.

对象A与对象X具有manyToOne关系.(注意,这是单侧关系,因此对象X无法看到对象A).

现在,我想获得对象A中列的最大值,但仅限于关系是特定子类型的位置,即... Y.

因此,这相当于...获取对象A中column1的最大值,跨越A的所有实例,它们与Y有关系.这可能吗?我有点迷失如何查询它.

我想的是:

String query = "SELECT MAX(a.columnName) FROM A a join a.x;
Query query = super.entityManager.createQuery(query);
query.execute();
Run Code Online (Sandbox Code Playgroud)

然而,这没有考虑X的子类...所以我有点迷失.

任何帮助将非常感激.

sql polymorphism hibernate jpa polymorphic-associations

5
推荐指数
2
解决办法
5603
查看次数

指向多个表之一的外键约束

我有一个表,其中一列source_id的值应为另一张表的主键,尽管它在哪张表中因记录而异。每个记录必须具有一个用于source_table指定源记录表的值,以及一个用于source_id指定源表中的行的值。

有什么方法可以利用数据库的外键约束和验证来完成此任务?还是我必须将验证逻辑移到应用程序层?或者,是否有另一种设计可以让我避免这个问题?

sql sqlite foreign-keys polymorphic-associations

5
推荐指数
1
解决办法
876
查看次数

Rails如何为多态关联填充"model_type"字段?

我有一个活动模型.它belongs_to :parent, :polymorphic => true.

是否Rails的使用parent.class.name,parent.model_name还是其他什么东西来填充PARENT_TYPE场?

我希望Presenter的行为与它包装的父对象相似,我需要覆盖正确的方法.

谢谢.

ruby-on-rails presenter polymorphic-associations ruby-on-rails-3

5
推荐指数
1
解决办法
1714
查看次数

通过Rails 3中的多态关系获取偏执狂删除的对象

我有一个Audit类,用于存储按属性的动作.

class Audit < ActiveRecord::Base
  attr_accessible :activity
  belongs_to :by, :polymorphic => true
  belongs_to :on, :polymorphic => true
  attr_accessible :by, :on
end
Run Code Online (Sandbox Code Playgroud)

for和:on的多态关联用于存储应审计的任何类型的对象.主要是因为多态在模式中被分解为类型和id,因此应该能够存储我的所有模型对象.

  create_table "audits", :force => true do |t|
    t.string   "activity"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "on_id"
    t.string   "on_type"
    t.integer  "by_id"
    t.string   "by_type"
  end
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是所有被审计的对象都使用了偏执狂宝石.偏执宝石在每个模型表中引入了deleted_at列,通过其default_scope检查该模型上的每个查询,default_scope设置为"where(deleted_at is null)".偏执宝石还提供了一种方法.with_deleted,它允许通过转动default_scope进行直接查询,因此也返回具有偏执/软删除的对象.

但是,如果我有任何已删除的项目,我会尝试使用列出的所有审核项目.

Audit.all
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何告诉Rails为每个多态的运行查询查询:by和:on对象添加.with_deleted调用.我的猜测是,rails通过查找多态关系的对象

eval(type).find(id)
Run Code Online (Sandbox Code Playgroud)

在我的情况下会给我一个应用了偏执宝石的default_scope的对象.

我试图在审计中覆盖self.find_by_sql,但没有运气.在我继续前进之前,我遇到了一些我需要阅读的Arel方法.

我看到以下解决方案,但我无法弄清楚如何做到这些.

  1. 覆盖多态查找.怎么样?
  2. 在评估之前将原始SQL作为字符串获取,并将where deleted_at为null部分的子/ gsub.

任何和所有关于如何处理这个问题的建议将不胜感激.

soft-delete polymorphic-associations ruby-on-rails-3

5
推荐指数
1
解决办法
1023
查看次数

Laravel Schema是否能够开箱即用地管理多态关联?

我想为项目创建一些可评论的模型,但我找不到任何创建评论迁移脚本的参考,我只在vimeo上发现了这个视频:Laravel 4 - Eloquent Collections&Polymorphic Relations.

我应该明确添加多态列吗?

Schema::create('comments',function($table){
    $table->increments('id');
    $table->text('body');
    $table->string('commentable_type');
    $table->integer('commentable_id');
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

问题来自于构建器在密钥是外键时期望程序员 $table->foreign('user_id')->references('id')->on('users');

php polymorphic-associations laravel

5
推荐指数
2
解决办法
2168
查看次数

为什么Rails orphan通过关联连接多态的记录?

提案文档可以分为许多不同的部分类型(文本,费用,时间表)等

这里使用连接表上的多态关联建模.

class Proposal < ActiveRecord::Base
  has_many :proposal_sections
  has_many :fee_summaries, :through => :proposal_sections, :source => :section, :source_type => 'FeeSummary'
end

class ProposalSection < ActiveRecord::Base
  belongs_to :proposal
  belongs_to :section, :polymorphic => true
end

class FeeSummary < ActiveRecord::Base
  has_many :proposal_sections, :as => :section
  has_many :proposals, :through => :proposal_sections 
end
Run Code Online (Sandbox Code Playgroud)

虽然#create工作正常

summary = @proposal.fee_summaries.create
summary.proposal == @propsal # true
Run Code Online (Sandbox Code Playgroud)

#new不要

summary = @proposal.fee_summaries.new
summary.proposal -> nil
Run Code Online (Sandbox Code Playgroud)

它应该返回零吗?

在常规的has_many和belongs_to初始化但尚未存在的记录仍将返回其父关联(内置在内存中).

为什么这不起作用,是否是预期的行为?

Schema.rb

 create_table "fee_summaries", :force => true do |t|
    t.datetime "created_at", :null => false
    t.datetime …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails polymorphic-associations ruby-on-rails-3

5
推荐指数
1
解决办法
304
查看次数

如何删除Eloquent中的多态关系?

我有这样的模型:

<?php

class Post extends Eloquent {
    protected $fillable = [];


    public function photos()
    {
        return $this->morphMany('Upload', 'imageable');
    }

    public function attachments()
    {
        return $this->morphMany('Upload', 'attachable');
    }

}
Run Code Online (Sandbox Code Playgroud)

我的morphMany表的架构是这样的:

CREATE TABLE `uploads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`raw_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`size` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`downloads` int(11) NOT NULL DEFAULT '0',
`imageable_id` int(11) DEFAULT NULL,
`imageable_type` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`attachable_id` int(11) DEFAULT NULL,
`attachable_type` …
Run Code Online (Sandbox Code Playgroud)

php polymorphic-associations laravel eloquent

5
推荐指数
2
解决办法
8541
查看次数

Rails 5 - 使用多态关联 - 呈现视图

我正在尝试学习如何在我的Rails 5应用程序中使用多态关联.我最近问了这个问题,但是我编辑了很多次来展示我正在尝试的所有东西,它变得一团糟

我的模型叫做Organization,Proposal和Package :: Bip.

协会是:

组织

has_many :bips, as: :ipable, class_name: Package::Bip
    accepts_nested_attributes_for :bips,  reject_if: :all_blank, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)

提案

has_many :bips, as: :ipable, class_name: Package::Bip
    accepts_nested_attributes_for :bips,  reject_if: :all_blank, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)

套票:必必

belongs_to :ipable, :polymorphic => true, optional: true #, inverse_of: :bip
Run Code Online (Sandbox Code Playgroud)

Package :: Bip可以与Organization或Proposal相关联.我正在努力弄清楚如何在我的提案节目中展示仅属于提案的Package :: Bips,以及组织的相同内容.

我的package :: bip表有以下两列:

#  ipable_id      :integer
#  ipable_type    :string
Run Code Online (Sandbox Code Playgroud)

ipable_type设置为Proposal或Organization.

在我的提案节目中,我有:

<% if @proposal.bips.present? %>
    <%#= render @proposal.bips %>
     <%= link_to proposal_package_bips_path(@proposal) do %> 
     <% end %>
<% end %> …
Run Code Online (Sandbox Code Playgroud)

ruby views ruby-on-rails polymorphic-associations

5
推荐指数
1
解决办法
702
查看次数