Tag: 多态关联

rails中的inheritance和多态关联

我有一个User模型,属于Profile (belongs_to polymorphic)。 一个模型有两个子类,但User中的profile_type始终对应于父模型。 User true SomeProf :profile SomeDeepProf1 < SomeProf SomeDeepProf2 < SomeProf 然后: sdp1 = SomeDeepProf1.new user = sdp1.create_user user.profile_type > ‘SomeProf’ 即使在子类中声明关联, profile_type仍然是SomeProf 。 为什么会这样? 有没有什么方法profile_type匹配子类而不是父类?

rails 3与回形针和多个模型的多态关联

我想与paperclip建立多态关联,并允许我的用户拥有一个头像和多个图像。 附件模型: class Attachment true end class Avatar { :thumb => “150×150>”, :view => “260×180>” }, end class Image { :thumb => “150×150>”, :view => “260×180>” }, end 用户模型: has_one :avatar, :as => :attachable, :class_name => ‘Attachment’, :conditions => {:type => ‘avatar’} accepts_nested_attributes_for :avatar 用户控制器: def edit @user.build_avatar end 用户视图表单: { :multipart => true } […]

Rails:包含多态关联

我读了这篇有趣的文章,关于使用多态来在Rails中创建更好的活动源 。 我们最终得到了类似的东西 class Activity < ActiveRecord::Base belongs_to :subject, polymorphic: true end 现在,如果其中两个主题是例如: class Event < ActiveRecord::Base has_many :guests after_create :create_activities has_one :activity, as: :subject, dependent: :destroy end class Image < ActiveRecord::Base has_many :tags after_create :create_activities has_one :activity, as: :subject, dependent: :destroy end 将create_activities定义为 def create_activities Activity.create(subject: self) end 客人和标签定义为: class Guest < ActiveRecord::Base belongs_to […]

关联特定类型的多态belongs_to

我对Rails比较新。 我想为使用多态关联的模型添加关联,但只返回特定类型的模型,例如: class Note < ActiveRecord::Base # The true polymorphic association belongs_to :subject, polymorphic: true # Same as subject but where subject_type is 'Volunteer' belongs_to :volunteer, source_association: :subject # Same as subject but where subject_type is 'Participation' belongs_to :participation, source_association: :subject end 我通过阅读关于ApiDock的关联尝试了大量的组合,但似乎没有任何东西完全符合我的要求。 这是我迄今为止最好的: class Note < ActiveRecord::Base belongs_to :subject, polymorphic: true belongs_to :volunteer, class_name: […]

使用整数ID类型字段的多态关联

我有一个表Foo ,它有一个名为bar的多态belongs_to关联。 foos表具有标准bar_id列。 但是,我有一个整数bar_type_id列,而不是基于字符串的bar_type_id列。 此列引用表bar_types的id列。 bar_types.name保存表示特定bar实例的类的类的名称。 Rails(理想情况下> = 2.3.10)是否允许这种类型的多态关联?

Rails 4多态关联和关注点

我正在尝试将Evaluation模型添加到我的Rails 4应用程序中。 我做了一个名为evaluation.rb的模型。 它有: class Evaluation true belongs_to :evaluatable, :polymorphic => true 我也对evaluator提出了关注并且可以evaluator为: 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 我在用户模型中包含了每个问题: class User < ActiveRecord::Base include Evaluator include Evaluatable 在我的展示页面中,我想展示特定用户的评估(从其他用户 […]

Rails多态has_many:通过

我从外部API中提取一些数据,并希望在本地缓存结果。 我有一个class SearchTerm ,我希望通过表searchable_items与几个不同的ActiveRecord类型相关联。 我很确定我的表格设置正确,但我的关联中的某些内容一定是错的。 class Foo :searchable, :through => :searchable_items end class Bar :searchable, :through => :searchable_items end class SearchTerm :searchable_items end class SearchableItem true end 我希望能够做一些像SearchTerm.find_by_term(‘SearchTerm’).searchables (它将返回一个Foo和Bar对象的数组)然而,我得到错误Could not find the association :searchable_items in model SearchTerm 提前感谢您提供给我的任何见解!

Ruby on Rails ::包含与子模型的多态关联

使用多态关联时,是否可以在仅存在于某些类型中的子模型上运行包含? 例: class Container belongs_to :contents, :polymorphic => true end class Food has_one :container belongs_to :expiration end class Things has_one :container end 在视图中我将要做的事情如下: 因此,当我加载c时,我想急切加载到期,因为我知道我将需要最后一个。 有没有办法这样做? 只定义一个常规:include得到我的错误,因为并非所有封闭类型都有子模型到期。

使用Rails多态关联形成用户注册

我在使用多态关联进行用户注册时遇到了问题。 我想从多个多态关联模型中显示字段,但我不能。 模型类: class User true end class Advisor :userable attr_accessible :extra accepts_nested_attributes_for :user # Idon’t know if it is ok end class AnotherKingOfUser < ActiveRecord::Base #the same.. end 我想为每个特定的用户类型创建一个控制器,所以: class AdvisorsController < ApplicationController # GET /advisors/new # GET /advisors/new.json def new # Here is where I'm not sure about what I did. I tried […]

评估 – 反馈回路的多态关联

我正试图弄清楚如何在我的Rails 4应用程序中实现评估模型。 我之前已经问过这些相关的问题,但我还没有解决这个问题: Rails 4多态关联和关注点 Rails 4 – 完成后评估模型 – 结构 我有: class Evaluation true belongs_to :evaluatable, :polymorphic => true 我也对评估者提出了关注并且可以评估为: 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 我在用户模型中包含了每个问题: class […]