Tag: 多态性

Rails多态与查找表

最好的方法是什么? 我希望能够通过多态性给乐队和艺术家带来流派。 我可以用habtm和has_many做到这一点:通过但是我试图弄清楚它是否可能通过多态性。 GenreList将是具有不同类型列表的查找表(例如Punk,Pop,Metal)。 我已经回顾了Ryan Bate关于Polymorphic Assoiciations的截屏video,但我仍然被卡住了。 具体来说,我不确定如何创建多态表Genre,它将从GenreList模型(查找表)中提供jar装类型。 以下是否正确? rails generate model Genre genre_list_id:integer genreable_id:integer genreable_type:string class Artist :genreable end class Band :genreable end class Genre true end class GenreList < ActiveRecord::Base end

如何加载多态模型?

我正在尝试优化我们的一些查询。 一个特别的挑战是试图加载多态模型。 在这种情况下, UserView是多态的,并具有以下列: user_id, user_viewable_id, user_viewable_type 当我尝试运行此查询时。 @user_views = UserView.select(‘user_views.* AS user_views, songs.* AS songs, users.* AS users’) .joins(‘INNER JOIN users AS users ON user_views.user_id = users.id’) .joins(‘INNER JOIN songs AS songs ON user_views.user_viewable_id = songs.id’) .where(:user_viewable_type => ‘Song’).order(‘user_views.id DESC’).limit(5) 它似乎并不急于加载查询。 我正在使用名为MiniProfiler的gem,它表示它实际上运行的是n + 1个查询,而不是一个。 以下AR查询返回此SQL: SELECT user_views.* AS user_views, songs.* AS songs, users.* AS […]

如何使用shoulda匹配器来测试多态分析?

我正在使用带有rails的shoulda-matcher,我正在创建一个名为“comments”的模型和另一个名为“post”的模型。 评论是多态的。 当我在post中使用shoulda匹配器进行测试时,就像这样 it {should have_many(:comments)} 得到这个消息 期望Post有一个名为comments的has_many关联(Comment没有post_id外键。) 在我的评论模型中,我有 belongs_to :commentable, :polymorphic => true 如何测试我的多态关联,以便post可以有很多注释? ps the shoulda matcher文档说它支持多态关联。

Ruby on Rails中的多态和forms

我最近一直都很满意,但多亏了这个令人敬畏的社区,我学到了很多东西。 我之前获得了多态关联所需的所有帮助,现在我有一个关于使用多态模型处理表单的问题。 例如,我有Phoneable和User,所以当我创建我的表单来注册用户时,我希望能够为用户分配一些电话号码(即:cell,work,home)。 class User :phoneable end class Phone true end class CreateUsers true end class CreatePhones true end 现在,当我创建表单时,我感到很困惑。 通常我会做以下事情: – form_for :user, :html => {:multipart => true} do |form| %fieldset %label{:for => “name”} Name: = form.text_field :name ##now how do I go about adding a phone number? ##typically I’d do this: ##%label{:for => […]

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 在我的展示页面中,我想展示特定用户的评估(从其他用户 […]

如何添加多态注释到Feed?

我正在尝试允许用户直接在他们的活动源上添加和查看评论,而不必去显示页面,这是我在这个railscast剧集中所做的: http ://railscasts.com/episodes/ 154-多态关联 – 修订 。 我从头开始创建活动源。 以下是我所做的尝试之一。 我将逻辑直接添加到索引操作中: class ActivitiesController < ApplicationController def index @activities = Activity.order("created_at desc").where(current_user.following_ids) @activity = Activity.find(params[:id]) @commentable = @activity @comments = @commentable.comments @comment = Comment.new end end 它告诉我: ActiveRecord::RecordNotFound in ActivitiesController#index Couldn’t find Activity with ‘id’= 我试图直接在活动索引中呈现注释部分: 评论/意见和评论/表格 Comment activity.rb class Activity < ActiveRecord::Base belongs_to :user has_many :comments, […]

Rails路由和控制器参数

我有两个共享相同控制器的资源。 到目前为止,我的方法是使用特殊的类型参数进行路由: resources :bazs do resources :foos, controller: :foos, type: :Foo resources :bars, controller: :foos, type: :Bar end 路线按预期工作,但我的所有链接都是这样的: /bazs/1/foos/new?type=Foo /bazs/1/bars/new?type=Bar 代替 /bazs/1/foos/new /bazs/1/bars/new 如何在不弄乱链接的情况下将参数传递给控制器​​?