简体   繁体   English

多态关联的未定义方法“类型”

[英]Undefined method `type' for Polymorphic Association

Undefined method `type' for Polymorphic Association. 多态关联的未定义方法“类型”。

My Polymorphic association with the models Follow, Dad and Mom gives me an error when writing a method to see if a user is already following one of them. 我与模型Follow,Dad和Mom的多态关联使我在编写一种方法来查看用户是否已经关注其中的一个方法时出现错误。

Here are all of my models and the method following which is the problem. 这是我所有的模型以及随之following的问题所在的方法。

Models 楷模

class User
 has_many :follows

  def following?(followable)
    follows.where(followable_id: followable.id, followable_type: followable.type ).any?
  end
end

class Mom
  has_many :follows, as: :followable, dependent: :destroy
end

class Dad
  has_many :follows, as: :followable, dependent: :destroy
end

Controller 调节器

@dad = Dad.find(params[:dad_id])

View 视图

<% unless current_user.following?(@dad) %>
  <%= link_to({ controller: 'follows', action: 'create', id: @dad.id }, { method: :post }) do %>
    Follow
  <% end %>
<% end %>

Error 错误

NoMethodError - undefined method `type' for #<Dad:0xaf63038>:
  activemodel (4.0.2) lib/active_model/attribute_methods.rb:439:in `method_missing'
  activerecord (4.0.2) lib/active_record/attribute_methods.rb:155:in `method_missing'
  app/models/user.rb:17:in `following?'
  .................

This works if I get rid of followable_type but that removes the reason for the method. 如果我摆脱了followable_type ,这将起作用,但是这消除了该方法的原因。 Why is this happening? 为什么会这样呢?

This line is the problem 这条线是问题

follows.where(followable_id: followable.id, followable_type: followable.type ).any?

Replace followable.type with followable.class.name followable.type替换followable.class.name

type used to be a synonym for class but this was deprecated. type以前是class的同义词,但已弃用。 Even if it hadn't been deprecated, you need to find records where followable_type is the name of the class, not the class itself. 即使未弃用它,您也需要查找记录,其中followable_type是类的名称 ,而不是类本身。

@MaxWilliams is correct although if you still want to use type you just need to include the method in your model @MaxWilliams是正确的,尽管如果您仍想使用type ,则只需在模型中包括该方法

module Typeify
  def type
    self.class.name
  end
end
class Mom
  include Typeify
end 
class Dad
  include Typeify
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多态关联未定义方法“ build_product” - Polymorphic association undefined method `build_product' 多态关联中的rails 5载波未定义方法缓存 - rails 5 carrierwave in polymorphic association undefined method cache 未定义的方法 `commentable' for #<activerecord::associations::collectionproxy []> (通过多态关联访问可注释的 id 和类型)</activerecord::associations::collectionproxy> - undefined method `commentable' for #<ActiveRecord::Associations::CollectionProxy []> (accesing commentable id and type via polymorphic association) 在Rails 3中使用多态关联的form_for中未定义的方法 - undefined method in form_for using polymorphic association in Rails 3 与:inverse_of的多态ActiveRecord关联的“未定义方法&#39;scan&#39;for nil:NilClass” - “undefined method `scan' for nil:NilClass” for polymorphic ActiveRecord association with :inverse_of 在build_association上获取未定义的方法以实现多态has_one关联 - Getting undefined method on build_association for polymorphic has_one association 在Rails中验证多态关联类型 - Validating polymorphic association type in Rails 按视图类型过滤多态关联 - Filtering polymorphic association by type for a view 获取特定类型的多态关联 - Get specific type of polymorphic association 在多态关联中更改类型标识符 - Change type identifier in polymorphic association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM