标签: polymorphic-associations

Rails多态关系和link_to

这是我的架构

class Menu < ActiveRecord::Base
  belongs_to :menuable, :polymorphic => true
end

class Page < ActiveRecord::Base
  has_one :menu, :as => :menuable
end

class Links < ActiveRecord::Base
  has_one :menu, :as => :menuable
end
Run Code Online (Sandbox Code Playgroud)

我想使用link_to链接到菜单视图中的多态类,例如

<%= link_to menu.name, menu.menuable %>
Run Code Online (Sandbox Code Playgroud)

这有效,但是当我想要的是生成链接时,这将从数据库中检索可菜单对象.你可以想象如果我的菜单很大,这将会让我的应用程序陷入困境.

当我将可菜单字段视为多态时,Rails创建了menuable_type和menuable_id.我可以使用什么来生成多态页面的链接,而不是使用巨型switch语句编写辅助函数(例如,如果我有大量可菜单的'子类'?)

ruby ruby-on-rails polymorphic-associations

3
推荐指数
1
解决办法
1797
查看次数

Ransack是否像MetaSearch一样在搜索中支持相同的polymorhpic belongs_to关联?

我正在从MetaSearch gem迁移到Ransack gem以升级到Rails 3.1并且我在搜索多态关联时遇到问题.现有的MetaSearch语法不适用于Ransack,但我找不到任何提及任何语法更改的文档.或者是否在Ransack中支持此功能.

例如,从MetaSearch github页面,给出以下类:

class Article < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Post < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
  validates_presence_of :body
end
Run Code Online (Sandbox Code Playgroud)

您可以在表单中创建一个搜索字段(这显然是从Searchlogic借来的约定):

<%= f.text_field :commentable_article_type_body_contains %>
Run Code Online (Sandbox Code Playgroud)

我正在使用这种类型的语法,它在MetaSearch中完美运行,但是对于Ransack,我的应用程序在查询参数包含此字段时抛出异常.例外是:

ActiveRecord::EagerLoadPolymorphicError (Can not eagerly load the polymorphic association :ownable)
Run Code Online (Sandbox Code Playgroud)

有谁知道如何在Ransack进行这种类型的搜索?

ruby-on-rails polymorphic-associations meta-search ransack

3
推荐指数
1
解决办法
2368
查看次数

使用MySQL Workbench创建多态关联

如何使用MySQL Workbench工具创建多态关系?我希望能够处理Rails给我的东西:

class Example < ActiveRecord::Base
  belongs_to :someone, polymorphic: true
end

class PolyOne < ActiveRecord::Base
  has_many :examples, as: :someone
end

class PolyTwo < ActiveRecord::Base
  has_many :examples, as: :someone
end
Run Code Online (Sandbox Code Playgroud)

mysql-workbench polymorphic-associations

3
推荐指数
1
解决办法
1520
查看次数

蒙古语嵌入关系中的实际多态性

我正在尝试使用Mongoid3将多态添加到我的嵌入关系中。

我有一个Item必须包含我的信息的embed_one对象的类。该协议是:
-我对象的类型可以是其中之一:CalendarStickerPicture
-无论对象的类型如何,我都想通过唯一的“键”来访问它:详细信息,
例如:

pry> my_item1.detail  
=> `<Picture _id: 1234>`  
pry> my_item2.detail  
=> `<Sticker _id: 8964>`
pry>
Run Code Online (Sandbox Code Playgroud)

首先,我尝试使用关键字as和此处描述的多态https : //github.com/mongoid/mongoid/issues/902
例如:

class Item
    include Mongoid::Document
    embeds_one  :detail, as: :item_slot
end

class Picture
    include Mongoid::Document
    embedded_in :item_slot, polymorphic: true
end

class Calendar
    include Mongoid::Document
    embedded_in :item_slot, polymorphic: true
end

class Sticker
    include Mongoid::Document
    embedded_in :item_slot, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)

然后,我尝试访问我的详细信息,但不幸的是,我收到此消息错误:

pry(main)> i = …
Run Code Online (Sandbox Code Playgroud)

ruby polymorphic-associations mongoid

3
推荐指数
1
解决办法
2188
查看次数

Laravel WhereHas 与多态关系

似乎 whereHas 方法不能很好地工作。

$res = Entreprise::whereHas('labels',function ($q)
    {
        $q->where('hidden','!=',1);
    })
    ->whereHas('labels',function ($q)
    {
        $q->whereHidden(1);
    })
    ->get();
 dd(count($res));  //shows int 2
Run Code Online (Sandbox Code Playgroud)

这是标签关系:

public function labels()
{
    return $this->morphToMany('Label', 'labelable');
}
Run Code Online (Sandbox Code Playgroud)

这是数据库:

id | nom                | deleted_at | created_at          | updated_at          | junior_id | label_type_id | abbreviation | id_siaje | hidden 
 6 | Environnord        | 0000-00-00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |         1 |             4 | EnvNord      |        0 |      1 
 7 | Salon créer        | 0000-00-00 | 0000-00-00 00:00:00 | …
Run Code Online (Sandbox Code Playgroud)

php mysql polymorphic-associations laravel laravel-4

3
推荐指数
1
解决办法
3131
查看次数

通过与STI的多态关联"has_many:through"关联

我有两个使用该people表的模型:PersonPerson::Employee(继承自Person).该people表有一type列.

还有另一个模型,Group它具有多态关联,称为:owner.该groups表包含owner_id列和owner_type列.


应用程序/模型/ person.rb:

class Person < ActiveRecord::Base
    has_one :group, as: :owner
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/人/ employee.rb:

class Person::Employee < Person
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ group.rb:

class Group < ActiveRecord::Base
    belongs_to :owner, polymorphic: true
    belongs_to :supervisor
end
Run Code Online (Sandbox Code Playgroud)

问题是,当我使用以下代码创建Person :: Employee时,该owner_type列设置为不正确的值:

group = Group.create
=> #<Group id: 1, owner_id: nil, owner_type: nil ... >
group.update owner: Person::Employee.create
=> true
group
=> …
Run Code Online (Sandbox Code Playgroud)

ruby model ruby-on-rails polymorphic-associations ruby-on-rails-4

3
推荐指数
1
解决办法
1930
查看次数

通过其多态关联的属性对Rails模型进行排序

我想这是一个重复的问题,但我还没有发现它,所以我想我会问.我有一个User模型Profile通过多态关联不同类型的模型.就像是:

class User < ActiveRecord::Base
  belongs_to :profile, polymorphic: true
end

class Profile < ActiveRecord::Base
  has_one :user, as: :profile
end

class FacebookProfile < ActiveRecord::Base
  has_one :user, as: :profile
end
Run Code Online (Sandbox Code Playgroud)

我想在User上执行一个查询,返回按其个人资料对象的名字排序的用户.在没有任何多态关系之前,我可以通过以joinson 开头:profile,但我知道这不适用于多态.

除了使用sortsort_by喜欢之外,还有更好的方法吗?

User.all.sort_by {|user| user.profile.first_name }
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails polymorphic-associations active-record-query

3
推荐指数
1
解决办法
1293
查看次数

使用 sqlalchemy 多态关系删除整个层次结构

我想删除sqlalchemy中具有多态关系的表中的一些元素。这是模型:

class Employee(Base):
  __tablename__ = 'employee'
  id = Column(Integer, primary_key=True)
  name = Column(String(50))
  type = Column(String(50))

  __mapper_args__ = {
    'polymorphic_identity':'employee',
    'polymorphic_on':type
  }

class Engineer(Employee):
  __tablename__ = 'engineer'
  id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
  engineer_name = Column(String(30))

  __mapper_args__ = {
    'polymorphic_identity':'engineer',
  }
Run Code Online (Sandbox Code Playgroud)

这是我删除它的方法:

e = Engineer();
e.name = "John";
e.engineer_name = "Doe";
DBSession.add(e);

q = session.query(Engineer).filter(Employee.name == "John")

q.delete(False)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误,这是一个错误还是我的操作方式错误?

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such
column: employee.name [SQL: u'DELETE FROM engineer WHERE employee.name
= ?'] [parameters: ('John',)]
Run Code Online (Sandbox Code Playgroud)

我期望 sqlalchemy 删除工程师和员工表中的条目。

python polymorphism sqlalchemy polymorphic-associations

3
推荐指数
1
解决办法
1768
查看次数

Laravel多态数据库Seeder工厂

如何为以下配置创建数据库种子工厂?

用户

// create_users_table.php
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    ...
}

// User.php
public function notes()
{
    return $this->morphMany('App\Note', 'noteable');
}
Run Code Online (Sandbox Code Playgroud)

复杂

// create_complex_table.php
Schema::create('complex', function (Blueprint $table) {
    $table->increments('id');
    ...
}

// Complex.php
public function notes()
{
    return $this->morphMany('App\Note', 'noteable');
}
Run Code Online (Sandbox Code Playgroud)

笔记

// create_notes_table.php
Schema::create('notes', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('noteable_id');
    $table->string('noteable_type');
    ...
}

// Note.php
public function noteable()
{
    return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)

我正在努力寻找最可靠的方法来确保我不只是填写id可能不存在的random 。

polymorphic-associations laravel-5 laravel-seeding

3
推荐指数
3
解决办法
2074
查看次数

Laravel hasManyThrough 多态关系

我有一个包含事务的表,其中每个事务都Transaction属于 aDriver或 a Customer- 所以我在它们之间设置了多态关系。

对于交易我已经设置:

public function owner() {
    return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)

对于司机和客户:

public function transactions() {
    return $this->morphMany(Transaction::class, 'owner');
}
Run Code Online (Sandbox Code Playgroud)

但每个驱动程序也属于一个Company. 我正在尝试获取属于Company直通hasManyThrough关系的所有交易:

public function transactions() {
    return $this->hasManyThrough(Transaction::class, Driver::class);
}
Run Code Online (Sandbox Code Playgroud)

但它似乎不适用于多态关系,因为它会抛出错误,因为它试图driver_idtransactions表中查找字段。

通过驱动程序获取属于公司的所有交易的方法是什么?

relationship has-many-through polymorphic-associations laravel eloquent

3
推荐指数
1
解决办法
1700
查看次数