我正在使用 mean.js 样板制作应用程序。它是一个以用户为雇主和候选人的招聘引擎。现在雇主和候选人应该存储为单独的文档,每个用户必须只有一个雇主或候选人,因为每个候选人或雇主都需要使用用户对象登录。
我用 postgresql 在 rails 中这样做:
## User
belongs_to :profileable, :polymorphic => true
## Candidate and Employer
has_one :user, :as => :profileable
Run Code Online (Sandbox Code Playgroud)
对于 mongoose,这是我到目前为止使用包“mongoose-schema-extend”所做的:
var extend = require('mongoose-schema-extend');
var UserSchema = new Schema({
email: {
type: String
},
password: {
type: String
}
}, { collection: 'users', discriminatorKey : '_type' });
var CandidateSchema = UserSchema.extend({
Experience: {
type: Number
}
});
var EmployerSchema = user.user.extend({
companyName: {
type: String
}
});
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但在 mongodb 中只为 _type 属性设置为候选人或雇主的用户创建文档。
我认为它应该做的是将候选人、雇主和用户存储为单独的文件。我后来必须将雇主与公司和工作联系起来。此外,我需要使用不同的标准分别查询候选人和雇主。
实现功能的最佳方法是什么?
所以我有一个名为的表files,其中包含一个文件列表,其中包含各自的名称、路径和文件类型。然后我还有一些其他表,可以附加文件。例如表user_profiles。最后,我有一个数据透视表,用于文件和其他表之间的多对多多态关系。数据透视表被称为fileables(想不出更好的名字)。现在,用户可能会在他们的个人资料中附加一些图片,可能还有一些视频,它们都来自文件。
通常,如果它只是图像,我会做这样的事情:
class UserProfile extends Model {
public function images()
{
return $this->morphToMany('App\File', 'fileable');
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于它是图像和视频,我想做这样的事情:
class UserProfile extends Model {
public function images()
{
return $this->morphToMany('App\File', 'fileable')->where('type', 'LIKE', 'image%');
}
public function videos()
{
return $this->morphToMany('App\File', 'fileable')->where('type', 'LIKE', 'video%');
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。那么这样做的正确方法是什么?
我有三个模型:用户、组织和角色。一个用户可以通过角色访问多个组织,一个组织可以通过角色拥有多个用户。此外,用户可以通过角色访问其他模型,因此角色模型有一个名为“access_to”的多态belongs_to关联,角色表有字段“user_id”、“access_to_id”和“access_to_type”来跟踪关联。这一切都很好,我可以使用organization.users和user.organizations集合并获取预期的记录。
但是,已决定将组织模型重命名为“Organization”,使用美国英语拼写而不是英国英语(这是一个虚构的示例,实际问题类似,但具有与此问题无关的额外复杂性)。该应用程序已运行多年,因此角色表中有数千条记录,其中“access_to_type”设置为“Organization”(带有“s”)。此外,“组织”模型必须保留以用于遗留代码,而“组织”模型则由新代码使用。
为了实现这一点,“source_type: 'Organization'” 通过以下方式添加到 has_many 中: User 和新的 Organization 模型的关联,因此完整的代码如下所示:
class Role < ApplicationRecord
belongs_to :user
belongs_to :access_to, polymorphic: true
end
class User < ApplicationRecord
has_many :roles, autosave: true, foreign_key: "user_id"
has_many(
:organizations,
through: :roles,
source: :access_to,
source_type: "Organisation"
)
end
class Organization < ApplicationRecord
self.table_name = 'organisations'
has_many :roles, as: :access_to
has_many :users, through: :roles, source: :access_to, source_type: "Organisation"
end
class Organisation < ApplicationRecord
has_many :roles, as: :access_to
has_many :users, through: :roles
end
Run Code Online (Sandbox Code Playgroud)
调用“User.first.organizations”仍然按预期工作,并使用以下 SQL 语句返回预期记录:
class Role …Run Code Online (Sandbox Code Playgroud) polymorphism activerecord ruby-on-rails polymorphic-associations
我正在开发一个多站点CMS,它具有站点间交叉发布的概念.几种类型的内容(文章,事件,Bios等)可以与许多网站相关联,而网站可以包含许多内容.内容片段和站点之间的多对多关联还必须支持每个相关内容项的一些共同属性 - 站点起源的概念(这是内容出现的原始站点吗?)以及概念给定关联网站上给定内容的"主要"和"次要"内容状态.
我的想法是创建一个名为ContentAssociation的多态连接模型,但是我很难让多态关联按照我的预期行事,而且我想知道我是否会这样做是错的.
这是我对连接表和模型的设置:
create_table "content_associations", :force => true do |t|
t.string "associable_type"
t.integer "associable_id"
t.integer "site_id"
t.boolean "primary_eligible"
t.boolean "secondary_eligible"
t.boolean "originating_site"
t.datetime "created_at"
t.datetime "updated_at"
end
class ContentAssociation < ActiveRecord::Base
belongs_to :site
belongs_to :associable, :polymorphic => true
belongs_to :primary_site, :class_name => "Site", :foreign_key => "site_id"
belongs_to :secondary_site, :class_name => "Site", :foreign_key => "site_id"
belongs_to :originating_site, :class_name => "Site", :foreign_key => "site_id"
end
class Site < ActiveRecord::Base
has_many :content_associations, :dependent => :destroy
has_many :articles, :through => :content_associations, :source …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails polymorphic-associations ruby-on-rails-3
为什么会这样
@discussions = Discussion.where(:discussable => @discussable)
Run Code Online (Sandbox Code Playgroud)
不起作用.然而这项工作:
@discussions = Discussion.where(:discussable_id => @discussable.id, :discussable_type => @discussable.class.to_s)
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有一个(多态)对象Comment(将用于Vehicle和Review对象).我怎样才能得到所有comments的User的VehicleS: @user.vehicles.comments?它说该方法comments未定义ActiveRecord::Relation.任何简单的方法让它工作?是多对多的关系:许多车辆有很多评论?还是我错了?@user.vehicles.first.comments工作正常.
对象之间的关系(不完整):
User
has_many Vehicles.
Vehicle
belongs_to User.
has_many Comments (as commentable).
Comment
belongs_to Commentable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud) ruby activerecord many-to-many ruby-on-rails polymorphic-associations
我试图与Laravel建立多态关系.我已经阅读了文档并查看了相关代码,但似乎无法完成所有工作.我想附加photos到rooms一个房间可以有很多照片的地方.在type与关系工程有关,但id始终0.
为什么它的一部分会起作用,有人可以指出我的关系中有什么问题吗?
我认为问题与我在表格中创建数据的方式有关:
$photo = new Photo;
$photo->URL = 'thePhotoURL.extension';
$photo->save();
$room = new Room;
$room->name = 'Some Room Name';
// seems weird to me that I "save" the photo twice
// or am I just saving the relationship here?
// have tried the other relationship options too (associate, attach, synch)
$room->photos()->save($photo);
// have also tried:
// $room->photos()->save(new Photo(array('URL' => 'urlTest.com')));
// get error "URL"
$room->save();
Run Code Online (Sandbox Code Playgroud)
创建照片表:
public function up() …Run Code Online (Sandbox Code Playgroud) 我有一个多态关系,地址既可以由成员拥有,也可以由依赖者拥有.一切都看起来很棒,直到我意识到我不知道什么类型的物体正在创造它,除非我遗漏了什么.有没有办法告诉路由文件包含对象的类型?
楷模:
class Member < ActiveRecord::Base
has_one :address, as: :person, dependent: :destroy
end
class Dependent < ActiveRecord::Base
has_one :address, as: :person, dependent: :destroy
end
class Address < ActiveRecord::Base
belongs_to :person, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@person = ???
@address = Address.new(person: @person)
end
Run Code Online (Sandbox Code Playgroud)
目前路线:
resources :members do
resources :addresses, shallow: true
resources :dependents, shallow: true do
resources :addresses, shallow: true
end
end
Run Code Online (Sandbox Code Playgroud)
我有路由要在每个下面解决,但我需要检查params [:member_id]或params [:dependent_id].当我将笔记附加到一切时会发生什么.我可能在Rails中错过了一些简单的方法,任何建议都将非常感谢!
routes ruby-on-rails polymorphic-associations ruby-on-rails-4
我希望一个类通过多态关联属于其他类.
限制与特定类列表的关联的最佳方法是什么?
我正在考虑使用这样的自定义验证方法,但我不知道这是否真的是最好的主意:
class Feature < ActiveRecord::Base
belongs_to :featureable, polymorphic: true
validate :featurable_is_of_allowed_class
FEATURABLE_CLASSES = ["Country", "City", "Store", "Product"]
def featurable_is_of_allowed_class
if !FEATURABLE_CLASSES.include? featurable.class.name
errors.add(:featurable, "class not allowed for association")
end
end
end
Run Code Online (Sandbox Code Playgroud) validation activerecord ruby-on-rails polymorphic-associations
我在项目开始时创建了一个注释模型,但现在我已经意识到我需要创建一些多态关系,所以我也可以使用其他一些模型的注释.考虑到我已经拥有的代码等,我认为我可能更容易从头开始,所以我可以以正确的方式为我的新多态世界构建所有视图/控制器等.
我知道我可以rails destroy model comments实现这个目标,但我有两个问题:
ruby-on-rails models polymorphic-associations rails-migrations destroy
activerecord ×5
laravel ×2
polymorphism ×2
destroy ×1
eloquent ×1
laravel-4 ×1
laravel-5 ×1
many-to-many ×1
models ×1
mongodb ×1
mongoose ×1
node.js ×1
routes ×1
ruby ×1
validation ×1