由于某种原因,多态has_many :through关联的源类型始终为0,尽管设置了a :source_type.
这就是我的模特的样子......
富:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
Run Code Online (Sandbox Code Playgroud)
酒吧:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
Run Code Online (Sandbox Code Playgroud)
TaggedItem:
belongs_to :tag
belongs_to :taggable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
标签:
has_many :tagged_items
has_many :foos, :through => :tagged_items, :source => :taggable, :source_type => "Foo"
has_many :bars, :through => :tagged_items, :source => :taggable, :source_type => "Bar"
Run Code Online (Sandbox Code Playgroud)
尽可能接近我可以说这是一个非常精细的设置,我能够创建/添加标签,但taggable_type总是最终为0.
任何想法都是为什么?谷歌一无所获.
ruby-on-rails has-many-through polymorphic-associations ruby-on-rails-4
我收到错误无法急切加载多态关联:messageable_fromuser
online = Message.all.joins(:messageable_fromuser)
Run Code Online (Sandbox Code Playgroud)
我试过
online = Message.all.includes(:messageable_fromuser)
Run Code Online (Sandbox Code Playgroud)
但它不包括结果中连接的表。使用包含时,我在日志中看到两个查询。我不知道为什么人们建议使用包含来急切加载。两个查询如何连接任何东西?
我正在尝试将Evaluation模型添加到我的Rails 4应用中.
我做了一个名为的模型evaluation.rb.它有:
class Evaluation < ActiveRecord::Base
belongs_to :evaluator, :polymorphic => true
belongs_to :evaluatable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
我也提出了担忧evaluator,并evaluatable为:
module Evaluator
extend ActiveSupport::Concern
included do
has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation'
end
end
module Evaluatable
extend ActiveSupport::Concern
included do
has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name: 'Evaluation'
end
end
Run Code Online (Sandbox Code Playgroud)
我在用户模型中包含了每个问题:
class User < ActiveRecord::Base
include Evaluator
include Evaluatable
Run Code Online (Sandbox Code Playgroud)
在我的展示页面中,我想展示特定用户的评估(从其他用户 - 他们是评估者)收到.
在我的节目中,我有:
<% Evaluation.find(params[:id]).evaluations.order('created_at DESC').each do |eval| %>
<div …Run Code Online (Sandbox Code Playgroud) 我有两个模型(项目和主题).它们都归第三个模型所有,具有has_many关联的用户(用户拥有许多主题和项目).项目和主题都具有多种:图像.
Image模型是一个多态关联,因此该表的列为imageable_id和imageable_type.如果我同时拥有ID为1的Item和ID为1的Theme,则该表将如下所示
id imageable_id imageable_type
------------------------------------
1 1 Item
2 1 Theme
Run Code Online (Sandbox Code Playgroud)
我正在使用declarative_authorization来重写我的数据库的SQL查询,以防止用户访问其帐户之外的项目.我想编写一个授权规则,允许用户只有在他们可以阅读他们拥有的项目时才能阅读图像.我似乎无法获得正确的语法(也许它不受支持):
has_permission_on [:images], :to => [:manage], :join_as => :and do
if_attribute :imageable => is { "Item" }
if_permitted_to :manage, :items # Somehow I need to tell declarative_auth to imageable_id is an item_id in this case.
end
Run Code Online (Sandbox Code Playgroud)
然后我会有另一个模仿上述但主题的规则:
has_permission_on [:images], :to => [:manage], :join_as => :and do
if_attribute :imageable => is { "Theme" }
if_permitted_to :manage, :themes # Somehow I need to tell declarative_auth to imageable_id is a theme_id …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails polymorphic-associations declarative-authorization
我想与paperclip建立多态关联,并允许我的用户拥有一个头像和多个图像.
附件模型:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end
class Avatar < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
class Image < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
Run Code Online (Sandbox Code Playgroud)
用户模型:
has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
Run Code Online (Sandbox Code Playgroud)
用户控制器:
def edit
@user.build_avatar
end
Run Code Online (Sandbox Code Playgroud)
用户视图表单:
<%= form_for @user, :html => { :multipart => true } do …Run Code Online (Sandbox Code Playgroud) ruby-on-rails paperclip polymorphic-associations ruby-on-rails-3
我有3个型号Page,Course和Content
Page并Course包含元数据并Content包含HTML内容.
Page与Course这两个的hasManyContent
Content属于Page和Course
为了避免page_id和和course_id字段Content(因为我希望这可以扩展到超过2个模型)我正在寻找使用多态关联.我开始使用面包店中的多态行为,但它正在为我喜欢产生太多的SQL查询,它也抛出了"非法偏移"错误,我不知道如何解决(它写于2008年,似乎没有人最近提到它也许错误是由于它没有为Cake 2设计的?)
无论如何,我发现我几乎可以通过硬编码模型中的关联来做我需要的一切:
页面模型
CREATE TABLE `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Page extends AppModel {
var $name = 'Page';
var $hasMany = …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何命名多态关联,而不是提出好的选择.将调用包含多态关联的类,LicenseBatch如果它不是多态的,则外键将是owner_id.看起来这个多态的名字会是ownerable,但是如果我有另一个可以拥有的类呢?我希望这更具体,但batch_ownerable听起来很尴尬.
您之前看到的任何建议或类似情况?
我在名为Notifiable的模型中命名了一个ploymorphic关联Notifiaction:
module Notifiable
def self.included(base)
base.instance_eval do
has_many :notifications, :as => :notifiable, :inverse_of => :notifiable, :dependent => :destroy
end
end
end
class Bill < ActiveRecord::Base
include Notifiable
end
class Balance < ActiveRecord::Base
include Notifiable
end
class Notification
belongs_to :notifiable, :polymorphic => true
belongs_to :bill, foreign_key: 'notifiable_id', conditions: "notifiable_type = 'Bill'"
belongs_to :balance, foreign_key: 'notifiable_id', conditions: "notifiable_type = 'Balance'"
end
Run Code Online (Sandbox Code Playgroud)
当我尝试加入通知时通知(Notification.joins{notifiable}- 它是吱吱声,活动记录代码会有相同的结果)我得到错误:ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :notifiable
我已经看过一些有关此异常的帖子,但当我尝试加入表格时,它们都不是我的情况.可能吗?我错过了什么
我有与此处描述的完全模式相同的多态连接表:http://aaronvb.com/articles/a-polymorphic-join-table.html
class Location < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class Checkpoint < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class NoteJoin < ActiveRecord::Base
belongs_to :notable, polymorphic: true
belongs_to :note
end
class Note < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :notable, polymorphic: true
belongs_to :user
has_many :note_joins
end
Run Code Online (Sandbox Code Playgroud)
我希望能够一次创建和更新多种类型的多态关联,而不必执行@note.locations << Location.first或更新@note.checkpoints << Checkpoint.first.
有点像@note.create note_joins_params并且@note.update note_joins_params会很棒的东西.
到目前为止,我能够实现创建部分的方法是将一系列属性传递给@note.note_joins.create,例如:
note_joins_params = [{"notable_id"=>"9225", "notable_type"=>"Location"}, …Run Code Online (Sandbox Code Playgroud) 我需要一些使用"多态关联"的学说2的帮助.让我澄清一下自己.实体可以使用多态关系的子集来支持文件附件.File实体用于保护这种关系,其中对文件的引用作为记录存储在files表中,并与父模型具有多态关系.我想创建与https://octobercms.com/docs/database/attachments相同的功能 但是不知道如何建立关系,以及如何使attachment_type像attachment_id一样动态;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToOne(targetEntity="App\Domain\FileAttachment\Entity\FileAttachment", attachment_type="news_thumbnail")
*/
private $thumbnail;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToOne(targetEntity="App\Domain\FileAttachment\Entity\FileAttachment", attachment_type="news_image")
*/
private $image;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="App\Domain\FileAttachment\Entity\FileAttachment", attachment_type="news_files")
*/
private $files;
Run Code Online (Sandbox Code Playgroud)
文件表的一个例子.
join ×2
polymorphism ×2
ruby ×2
activerecord ×1
cakephp ×1
doctrine-orm ×1
filesystems ×1
paperclip ×1
symfony ×1