这是我的架构
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语句编写辅助函数(例如,如果我有大量可菜单的'子类'?)
我正在从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进行这种类型的搜索?
如何使用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) 我正在尝试使用Mongoid3将多态添加到我的嵌入关系中。
我有一个Item必须包含我的信息的embed_one对象的类。该协议是:
-我对象的类型可以是其中之一:Calendar,Sticker,Picture,
-无论对象的类型如何,我都想通过唯一的“键”来访问它:详细信息,
例如:
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) 似乎 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) 我有两个使用该people表的模型:Person和Person::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
我想这是一个重复的问题,但我还没有发现它,所以我想我会问.我有一个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,但我知道这不适用于多态.
除了使用sort或sort_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
我想删除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 删除工程师和员工表中的条目。
如何为以下配置创建数据库种子工厂?
用户
// 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 。
我有一个包含事务的表,其中每个事务都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_id在transactions表中查找字段。
通过驱动程序获取属于公司的所有交易的方法是什么?
relationship has-many-through polymorphic-associations laravel eloquent
ruby ×3
laravel ×2
eloquent ×1
laravel-4 ×1
laravel-5 ×1
meta-search ×1
model ×1
mongoid ×1
mysql ×1
php ×1
polymorphism ×1
postgresql ×1
python ×1
ransack ×1
relationship ×1
sqlalchemy ×1