我对我所获得的多态关联感到有些困惑.我需要一个文章模型来获得标题图像和许多图像,但我希望有一个图像模型.更令人困惑的是,Image模型具有多态性(允许其他资源拥有许多图像).
我在我的文章模型中使用此关联:
class Article < ActiveRecord::Base
has_one :header_image, :as => :imageable
has_many :images, :as => :imageable
end
Run Code Online (Sandbox Code Playgroud)
这可能吗?谢谢.
只是想找出为以下场景设计表的最佳方法:
我的系统中有几个区域(文档,项目,组和客户端),每个区域都可以记录注释.
我的问题是我应该有这样一个表:
CommentID
DocumentID
ProjectID
GroupID
ClientID
etc
Run Code Online (Sandbox Code Playgroud)
只有一个id会有数据,其余的将是NULL,或者我应该有一个单独的CommentType表,并且我的注释表如下:
CommentID
CommentTypeID
ResourceID (this being the id of the project/doc/client)
etc
Run Code Online (Sandbox Code Playgroud)
我的想法是,从索引的角度来看,选项2会更有效.它是否正确?
目前我正在开发一个RFID项目,每个标签都附在一个物体上.一个物体可以是一个人,一台电脑,一支铅笔,一个盒子或者老板心中的任何东西.当然,每个对象都有不同的属性.
所以我想要一个表标签,我可以在其中保存系统中每个标签的寄存器(标签的注册).另外一个表格,我可以将标签与对象相关联并描述其他一些属性,这就是我们所做的.(没有真正的架构只是一个简化版本)

突然间,我意识到这个模式在几个表中可以有相同的标记.例如,标签123可以同时在C和B中.这是不可能的,因为每个标签只能附加到一个对象上.
简单来说,我希望每个标记在数据库中不会出现多次.
我目前的做法

我真正想要的是什么

更新: 是的,TagID由最终用户选择.此外,TagID由标签阅读器给出,TagID是128位数.
新更新: 到目前为止的对象是:
- 药物(TagID,comercial_name,generic_name,amount,...)
- 机器(TagID,名称,描述,型号,制造商......)
- 患者(TagID,firstName,lastName,birthday,...)
所有属性(列或您命名的任何属性)都是非常不同的.
更新后更新
我正在研究一个带有医院RFID标签的系统.每个RFID标签都附着在一个物体上以便观察它们,不幸的是每个物体都有许多不同的属性.
对象可以是人,机器或药物,也可以是具有其他属性的新对象.
所以,我只想要一个灵活的切割器架构.这允许我引入新对象的类型,并让我轻松地向一个对象添加新属性.请记住,这个系统可能非常庞大.
例子:
Tag(TagID)
Medicine(generic_name, comercial_name, expiration_date, dose, price, laboratory, ...)
Machine(model, name, description, price, buy_date, ...)
Patient(PatientID, first_name, last_name, birthday, ...)
Run Code Online (Sandbox Code Playgroud)
我们必须只为一个对象关联一个标记.
注意:我真的不会说(或者也写):P抱歉.这里不是母语人士.
我需要(或者我认为)在我的模型中实现多态关联,但是我有些不对劲.让我们看看我的情况,这是一个简单的问题/答案系统,逻辑如下: - 一个问题可以通过N个答案回答. - 答案可以只是"文本"XOR(一个或另一个,而不是两个)"图片".
迁移:
class CreateAnswers < ActiveRecord::Migration
def change
create_table :answers do |t|
t.integer :question_id
t.references :answerable, :polymorphic => true
t.timestamps
end
end
end
class CreateAnswerTexts < ActiveRecord::Migration
def change
create_table :answer_texts do |t|
t.text :content
t.timestamps
end
end
end
class CreateAnswerPictures < ActiveRecord::Migration
def change
create_table :answer_pictures do |t|
t.string :content
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
型号 *answer.rb*
class Answer < ActiveRecord::Base
belongs_to :user_id
belongs_to :question_id
belongs_to :answerable, :polymorphic => true
attr_accessible :answerable_type
end
Run Code Online (Sandbox Code Playgroud)
answer_text.rb …
从Rails 3.2升级到4.1后,以下的代码现在失败了:
在控制器/规范中:
post = user.posts.build
post.contacts << contact # contact is a persisted record
post.save! # now fails
Run Code Online (Sandbox Code Playgroud)
我基本上试图保存帖子及其相关的联系人,这应该是contact_publishment即时创建一个记录.错误在新contact_publishment记录上:"可发布不能为空"
该模型:
class Contact
...
has_many :contact_publishments
...
end
class ContactPublishment
...
belongs_to :publishable, polymorphic: true
belongs_to :contact
validates_uniqueness_of :publishable_id, :scope => [:contact_id, :publishable_type]
validates_presence_of :contact, :publishable
...
end
class Post
...
has_many :contact_publishments, as: :publishable
has_many :contacts, through: :contact_publishments
...
end
Run Code Online (Sandbox Code Playgroud) ruby-on-rails has-many-through polymorphic-associations ruby-on-rails-4
我的Rails应用程序中有一个非常简单的(我认为)单表继承(STI)设置.
有一个带有嵌套资源Post的User模型.使用STI,我有它,以便一些帖子可以是Post :: Urgent对象.
我注意到我的URL帮助程序<%= [@user, @post] %>需要硬编码,<%= user_post_path[@user, @post] %>否则我最终会看到有关Rails没有找到user_post_urgent_path方法的错误.好的,这相当容易.
(这样做的另一种方法也是<%= [@user, post: @post] %>.)
好吧,由于某种原因,我无法弄清楚如何以form_for相同的方式调整方法.当我写简单的时候
<%= form_for [@user, @post] do |f| %>
Run Code Online (Sandbox Code Playgroud)
,我在URL帮助器案例中得到了类似的错误,但是在表单上下文中:undefined method 'user_post_urgen_path'.
我通过指定:
<%= form_for [@user, @post], url: :user_post do |f| %>
Run Code Online (Sandbox Code Playgroud)
但这并不完全有效,因为当我提交表单时,我在控制器中遇到问题,说强参数行params.require(:post)失败:
param is missing or the value is empty: post
Run Code Online (Sandbox Code Playgroud)
如果我检查,params我发现表单正在传递一个post_urgent对象,而不是一个post对象.
当然,我可以手动输入一些总是说的逻辑if !params[:post] and params[:post_urgent], then params[:post] = params[:post_urgent].但是,这似乎只是方式太哈克,尤其是对Rails的. …
forms inheritance ruby-on-rails polymorphic-associations sti
如果我在3个模型之间有多态关联:
评论
belongs_to :book, :class_name => 'Book', :foreign_key => 'ref_id', conditions: "comments.ref_type = 'Book'"
belongs_to :article, :class_name => 'Article', :foreign_key => 'ref_id', conditions: "comments.ref_type = 'Article'"
belongs_to :ref, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
对于给定的注释列表,如何从Title两个列Book和Article模型中选择不同的值?
例如,如果我必须列出书籍的标题和在一段时间内给出评论的文章,那我该怎么做呢?我可以很容易地挑选评论列表,但我要如何挑选与独特的游戏,从Book和Article?
例如:
Book
+--------------+
| Id | Title |
+----+---------+
| 1 | 'Book1' |
| 2 | 'Book2' |
| 3 | 'Book3' |
+--------------+
Article
+-----------------+
| Id | Title |
+----+------------+
| 1 | 'Article1' …Run Code Online (Sandbox Code Playgroud) 由于某种原因,多态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) ruby ×2
sql-server ×2
activerecord ×1
database ×1
forms ×1
inheritance ×1
join ×1
polymorphism ×1
sql ×1
sti ×1