Tag: 多态关联

重构Form_for为多态关联中的注释创建方法

我正在研究我的第一个多态关联关系,我无法重构我的form_for创建注释。 我尝试通过Polymorphic Association RailsCasts http://railscasts.com/episodes/154-polymorphic-association?view=asciicast ,但似乎过时了。 我有两个问题: 如何重写我的comment_form部分,以便它适用于任何可评论的内容? 我现在的方式,它只适用于traveldeals (:commentable_id => @traveldeal.id) 。 当我创建注释时,commentable_type为空。 什么是commentable_type,我需要在表单中传递它吗? 谢谢! user.rb class User :destroy end traveldeal.rb class Traveldeal :commentable, :dependent => :destroy end comment.rb class Comment true validates :user_id, :presence => true validates :commentable_id, :presence => true validates :content, :presence => true end traveldeal_show.html.erb _comment_form.html.erb @traveldeal.id) do |f| %> […]

Rails如何为多态关联填充“model_type”字段?

我有一个活动模型。 它belongs_to :parent, :polymorphic => true 。 Rails是否使用parent.class.name , parent.model_name或其他东西来填充parent_type字段? 我希望Presenter的行为与它包装的父对象相似,我需要覆盖正确的方法。 谢谢。

具有多态模型的嵌套forms

我正在创建一个具有以下属性的应用程序,我正在创建一个单独的表单,以便能够保存目标,目标的任务,目标的里程碑和里程碑的任务。 #app/models/goal.rb has_many :tasks, :as => :achievement has_many :milestones accepts_nested_attributes_for :tasks accepts_nested_attributes_for :milestones #app/models/milestone.rb belongs_to :goal has_many :tasks, :as => :achievement #app/models/task.rb belongs_to :achievement, :polymorphic => true 每当我用它的属性保存目标时,似乎任务模型对它们所属的成就类型感到困惑,导致每个里程碑任务被列为目标任务。 我的表单,部分和控制器代码如下。 形成: f.object %> f %> ff %> ff %> milestone_fields partial: ff %> task_fields partial: 目标控制器: def new @goal = current_user.goals.new end def create @user = […]

用于嵌套索引操作的Rails多态链接

我一直试图找到这个链接几个小时。 我有一个多态关联,其中的集合和分类都有设计。 收集模型 has_many :designs, :as => :targetable 分类模型 has_many :designs, :as => :targetable 设计模型 belongs_to :targetable, :polymorphic => true 为了链接到设计的“show”动作,正确的多态路径将是: link_to polymorphic_path([@targetable, @design]) 但我无法弄清楚如何链接到设计的“索引”页面,以显示与其相应的可定位对象相关的所有设计。 有谁知道到达那里的适当链接?

Rails中的多态关联

多态关联如何在Rails中运行? 它们的优点是什么? 有没有办法只通过运行迁移添加belongs_to方法?

多态嵌套表单对象创建的未知属性

我试图通过fields_for创建一个关联的多态对象。 我得到一个未知的属性错误,虽然父母被创建时,@ order参数是不是以某种方式通过? 家长/订单控制器 – # GET /orders/new def new @order = Order.new @order.build_patient @order.product = OrthosisSpecification.new end # GET /hotels/1/edit def edit @order.build_patient end # POST /orders # POST /orders.json def create @order = Order.new(order_params) respond_to do |format| if @order.save format.html { redirect_to @order, notice: ‘Order was successfully created.’ } format.json { render :show, […]

Ransack在其搜索中是否支持与MetaSearch相同的多态性belongs_to关联?

我正在从MetaSearch gem迁移到Ransack gem以升级到Rails 3.1并且我在搜索多态关联时遇到问题。 现有的MetaSearch语法不适用于Ransack,但我找不到任何提及语法更改的文档。 或者是否在Ransack中支持此function。 例如,从MetaSearch github页面,给出以下类: class Article :commentable end class Post :commentable end class Comment true validates_presence_of :body end 您可以在表单中创建一个搜索字段(这显然是从Searchlogic借来的约定): 我正在使用这种类型的语法,它在MetaSearch中完美运行,但是对于Ransack,我的应用程序在查询参数包含此字段时抛出exception。 例外是: ActiveRecord::EagerLoadPolymorphicError (Can not eagerly load the polymorphic association :ownable) 有谁知道如何在Ransack进行这种类型的搜索?

has_many的源reflection错误:通过

我正在尝试创建一个系统,我的网站的用户可以在其中collections页面。 这些页面有两种类型,俱乐部或体育。 所以,我有四个模型,相关联: 用户模型: class User :favorites has_many :clubs, :through => :favorites .. end collections夹型号: class Favorite true end 俱乐部型号: class Club :favoritable has_many :users, :through => :favorites def to_param slug end end 运动模型: class Sport :favoritable has_many :users, :through => :favorites .. end 从本质上讲,用户可以通过collections夹进行体育或俱乐部,而collections,体育和俱乐部之间的关联是多态的。 在实践中,这一切都按照我想要的方式工作,并且我设计的整个系统都有效。 但是,我在我的网站上使用Rails_Admin,我在三个地方收到错误: 第一次加载仪表板(/ admin)时。 如果我刷新页面,它工作正常。 在Rails_Admin中加载用户模型时 在Rails_Admin中加载collections夹模型时 这是/admin/user (gist)上的错误消息。 所有错误都是类似的,引用ActiveRecord::Reflection::ThroughReflection#foreign_key […]

rails加入多态关联

我在名为Notifiaction的模型中有一个名为Notifiable的ploymorphic关联: 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 true belongs_to :bill, foreign_key: ‘notifiable_id’, conditions: “notifiable_type = ‘Bill'” belongs_to :balance, foreign_key: ‘notifiable_id’, conditions: “notifiable_type = ‘Balance'” end 当我尝试加入通知时通知( Notification.joins{notifiable} notifiable Notification.joins{notifiable} – 它是吱吱声,活动记录代码会有相同的结果)我得到错误: ActiveRecord::EagerLoadPolymorphicError: […]

如何为具有多态关联的模型优雅地构建表单?

这是我的模特: class Lesson true validates_presence_of :topic_type, :topic_id end class Subject :topic end class Category :topic end 现在,我需要的是一个允许用户创建或更新课程的表单。 问题是,我如何提供一个提供主题和类别混合的选择菜单? (对于用户而言,在此特定表格中,主题和类别是可互换的,但在其他地方并非如此。) 理想情况下,这看起来像这样: 意见/经验/ _form.html.haml = simple_form_for(@lesson) do |f| = f.input :title = f.association :topic, :collection => (@subjects + @categories) 这不起作用,因为我们只指定topic_id,我们也需要topic_types。 但是我们如何指定这些价值呢? 我想这个问题的症结在于我真的想要一个单一的选择菜单,它指定两个对应于两个不同属性的值(topic_id和topic_type)。 有没有优雅的方式来做到这一点? 几点说明: a)单表inheritance会使这个问题消失,但我想避免这种情况,因为类别和主题有他们自己的关系……我会饶有你的细节。 b)我可能会拉一些javascript shenanigans,是吗? 但这听起来很混乱,如果有一种更清洁的方式来做,一些神奇的forms助手或其他东西,那么这当然更可取。 c)虽然我使用的是simple_form,但我不会坚持使用它,以防万一这会让事情变得复杂。 谢谢