Tag: polymorphic associations

如何从rails表单中的对象关联提供数据?

我有一个对象,我正在开发一个具有许多属性的控制器。 但是,由于我创建的数据模型,我必须编辑的大多数属性都是通过其他表中的关联保存的。 EG:我的文章通过标签表有标签(其他表中保存了大约20个其他属性)。 该文章通过多态关联等具有许多其他属性。 关联工作得很好,并且可以保存每个属性的多个条目。 但是,有一件事很难将编辑操作简化为控制器。 通常你可以保存@article = Article.find(params [:id])并显示所有属性。 这意味着表单具有服务的当前属性,并且该人员可以进行更改。 但是,对于通过关联保存的内容,这些字段为空。 我有一个shiv解决方案,例如,@ article.tag_list = @ article.tags.map(&:name)。 这使字段现在显示标签。 但是,对每个属性执行此操作会为控制器中的编辑操作添加许多行。 有没有更好的办法? 如果有一些代码我应该发布,我可以 – 只是没有,因为它现在很乱,所以我想我会解释。

Rails,Polymorphic Association – 仅渲染关联实例

我正在尝试学习如何在我的Rails 5应用程序中使用多态关联。 我有一个名为Organization,Proposal和Package :: Bip的模型。 协会是: 组织 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 提案 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 套票:必必 belongs_to :ipable, :polymorphic => true, optional: true, inverse_of: :bip Package :: Bip可以与Organization或Proposal相关联。 我正在努力弄清楚如何在我的提案节目中展示仅属于提案的Package :: Bips,以及组织中的提案。 我的package :: bip表有以下两列: # ipable_id :integer # […]

has_one / has_many具有依赖的销毁但使用不同的密钥名称

所以我正在看某人的代码,其中包含以下内容(转述): class user has_one :connection, :dependent => :destroy has_one :second_user, :through => :connection, :class_name => ‘User’ end class connection belongs_to :user belongs_to :second_user, :class => ‘User’ end 如果我有一个连接对象并删除关联的“用户”,它可以被销毁。 但我也希望这样做,如果占用’second_user’字段的用户被破坏,连接就会被破坏。 如何在不浪费太多的情况下完美无缝地完成(希望不需要迁移)? 谢谢!

Rails:Polymorphic assosiation和accepts_nested_attributes

附件不会保存。 我在这里缺少什么? 在我的应用程序中,我有一个项目,每个项目用户都可以上传许多资产。 上传由载波完成。 这是模特 class Project :assetable,dependent: :destroy accepts_nested_attributes_for :assets, :allow_destroy => true end class Asset true mount_uploader :attachment, AttachmentUploader #carrierwave validates :attachment, presence: true validates :project_id, presence: true end 这些是我的project_controller中的动作 def new @project = Project.new @asset = @project.assets.build end def create @project = Project.new(project_params) @project.assets.build respond_to do |format| if @project.save format.html { redirect_to […]

多对多连接和关联

用户可以通过Clips Scaffold上传Clips Scaffold ,此剪辑属于Show和/或Album (显示控制器,相册控制器)。 用户可以拥有 只有一个节目 很多ALBUMS 我想要完成的事情: 用户可以上传剪辑 ,这些剪辑可以附加到“ 显示”或/和“ 相册” 有关更详细的概述,请查看下图: 问题#1我不理解的是我必须使用的关联。 我目前的型号: 用户模型 : has_many :clips, dependent: :destroy has_one :show, dependent: :destroy has_many :albums, dependent :destroy 剪辑型号 : belongs_to :user 显示模型 : belongs_to :user 专辑模型 : belongs_to :user ——————————————————————————— 问题#2我不确定如何完成,当用户上传剪辑时,他可以将它附加(或分配)给一个节目或专辑。 我对Show做了什么(因为只有一个)只是在View页面上调用。 这意味着用户上传的每个剪辑都会出现在Show上。 哪个错了…… 现在我有一个Show和一个专辑我必须让用户能够选择在哪里附加它(在他拥有的许多专辑之一,或他的节目)。 或者我应该使用一个解决方案,用户可以通过相册或显示控制器上传剪辑(而不是通过单独的剪辑控制器 ) 我真的需要一些帮助:)

渴望加载,具体取决于Ruby on Rails中的关联类型

我有一个多态关联( belongs_to :resource, polymorphic: true ),其中resource可以是各种不同的模型。 为简化问题,假设它可以是Order或Customer 。 如果是Order我想加载订单,并预加载Address 。 如果是客户,我想预先加载Customer并预加载Location 。 使用这些关联的代码执行如下操作: 目前我的预装用途: @issues.preload(:resource) 但是我仍然看到加载条件关联的n +加一个问题: SELECT “addresses”.* WHERE “addresses”.”order_id” = … SELECT “locations”.* WHERE “locations”.”customer_id” = … … 有什么好办法解决这个问题? 是否可以手动预加载关联?

Rails – 多态collections夹(用户可以喜欢不同型号)

我们正在尝试添加多个可接受的对象,用户可以在其中collections许多不同的对象,但不确定如何使其工作。 这是最喜欢的型号: class Favorite < ActiveRecord::Base # belongs_to :imageable, polymorphic: true belongs_to :user belongs_to :category belongs_to :business belongs_to :ad_channel belongs_to :location belongs_to :offer end 用户模型: class User < ActiveRecord::Base has_many :favorites, as: :favoritable end 还有一个可以collections的示例模型: class Category < ActiveRecord::Base has_many :sub_categories has_many :ad_channels has_many :offers belongs_to :favoritable, polymorphic: true end 我不确定这是否设置正确,这是我们首先需要一些反馈。 其次,我们如何为用户“喜欢”某些东西? 这是我们到目前为止尝试过的失败: @user.favorites << […]

路由关注和多态模型:如何共享控制器和视图?

鉴于路线: Example::Application.routes.draw do concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :forums do resources :forum_topics, concerns: :commentable end end 而型号: class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end 当我编辑或添加评论时,我需要回到“可评论”对象。 不过,我有以下几个问题: 1)根据父对象, comments_controller.rb的redirect_to会有所不同 2)对视图的引用也会有所不同 = simple_form_for comment do |form| 是否有实用的方法来共享此comment资源的视图和控制器?

STI和多晶型物

我的代码有问题 class Post < ActiveRecord::Base end class NewsArticle :commentable, :dependent => :destroy, :order => ‘created_at’ end class Comment true, :counter_cache => true end 并尝试去获取一些NewsArticle的评论,我在日志中看到类似的东西 Comment Load (0.9ms) SELECT “comments”.* FROM “comments” WHERE (“comments”.”commentable_id” = 1 and “comments”.”commentable_type” = ‘Post’) ORDER BY created_at 奇怪的是“commentable_type”=“发布” 。 怎么了? PS:Rails 2.3.5 && ruby​​ 1.8.7(2010-01-10 patchlevel 249)[i686-darwin10]

Ajax with Polymorphic Association(Destroy&Create)

我在rails应用程序中与dailyposts和comments有多态关联 一切都在数据库中工作正常,但当我尝试将Ajax添加到Destroy Action(我使用本教程http://railscasts.com/episodes/136-jquery-ajax-revised )….它不起作用除非我刷新页面。 (但我的警报在destroy.js.erb中有效) 我知道我的错误在destroy.js.erb中 新手Rails …请帮忙:) 这是我的代码…… ROUTES resources :dailyposts do resources :comments end CONTROLLERS ##Dailyposts class DailypostsController :created_at) @comment = Comment.new end end ##Comments class CommentsController < ApplicationController before_filter :load_commentable respond_to :html, :js def create @comment = @commentable.comments.create(params[:comment]) @comment.user = current_user if @comment.save respond_to do |format| format.html { redirect_to @commentable } format.js […]