Ruby on rails 模型关联问题:NoMethodError:未定义的方法`扩展';对于#<;哈希…>;

Ruby on rails 模型关联问题:NoMethodError:未定义的方法`扩展';对于#<;哈希…>;,ruby-on-rails,model,controller,ruby-on-rails-6,Ruby On Rails,Model,Controller,Ruby On Rails 6,我目前正在将我的rails 5.2应用程序升级到rails 6.0,同时遵循 在我的一个用户类(label或artist)与links模型交互时遇到错误之前,一切似乎都很正常 当我尝试以艺术家或标签身份注册时,当我需要定义指向用户社交媒体或网站的链接时,我会收到以下错误: Completed 500 Internal Server Error in 139ms (ActiveRecord: 7.3ms | Allocations: 14518) NoMethodError - unde

我目前正在将我的rails 5.2应用程序升级到rails 6.0,同时遵循

在我的一个用户类(label或artist)与links模型交互时遇到错误之前,一切似乎都很正常

当我尝试以艺术家或标签身份注册时,当我需要定义指向用户社交媒体或网站的链接时,我会收到以下错误:

Completed 500 Internal Server Error in 139ms (ActiveRecord: 7.3ms | Allocations: 14518)

    NoMethodError - undefined method `extensions' for #<Hash:0x000000000bdd4ec8>
    Did you mean?  extend:
      app/controllers/label/personal_informations_controller.rb:8:in `edit'
和我的标签型号:

class Label < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  include PgSearch::Model

  has_attached_file :logo, :s3_protocol => :https,
                    styles: { medium: "350x360#", thumb: "40x40#" },
                    default_url: ":class/:attachment/missing_:style.jpg"
  validates_attachment_content_type :logo, content_type: %r{\Aimage/.*\Z}
  alias_method :avatar, :logo

  has_and_belongs_to_many :genres
  has_many :feedback_requests
  has_many :payment_types, as: :payee
  has_many :links, as: :linkable, dependent: :destroy
  has_many :releases
  has_many :label_artists
  has_many :sent_messages, as: :from, class_name: "Message"
  has_many :messages, through: :conversations
  has_many :conversations
  has_many :songs, through: :feedback_requests, source: :song
  has_many :artist_contacts, through: :songs, source: :artist
  has_many :contracts, through: :conversations

  accepts_nested_attributes_for :payment_types
  accepts_nested_attributes_for :links, reject_if: ->(attributes) { attributes[:url].blank? }

  validates :name, :real_name, presence: true
  validates :city, :country_code, :profile, presence: true, on: :update

  delegate :name, to: :country, prefix: true, allow_nil: true

  pg_search_scope :search,
                  against: :name,
                  associated_against: { genres: :name },
                  using: {
                    tsearch: { dictionary: :english }
                  }

  pg_search_scope :by_relevance,
                  associated_against: { genres: :name },
                  using: {
                    tsearch: { dictionary: :english, any_word: true }
                  }

  def self.approved
    where("approved_at IS NOT NULL")
  end

  def approved?
    approved_at.present?
  end

  def payment_type
    payment_types.order("created_at").last
  end

  def display_name
    name
  end

  def country
    Country.new(code: country_code)
  end

  def location
    [city, country_name].compact.join(", ")
  end

  def logo_url(style = :medium)
    logo.url(style)
  end

  # The profit the label has earned (deducting Sendemo's cut)
  def balance_net
    ((raw_balance.to_i/100.0) * (1-Payment.sendemo_fee_fraction)).round(2) if raw_balance.present?
  end

  # A label's balance is the entire balance charged through the label including Sendemo's fee.
  # This is for backwards compatibility and should be changed
  def balance=(new_balance)
     write_attribute(:balance, (new_balance.to_f*100).to_i) if new_balance.present?
  end

  def balance
    (raw_balance.to_i/100.0).round(2) if raw_balance.present?
  end

  def raw_balance
    read_attribute(:balance)
  end

  def unread_messages_count
    messages.unread.count
  end

  def unread_messages?
    unread_messages_count > 0
  end
end
类标签:https,
风格:{中等:“350x360#”,拇指:“40x40#”},
默认的_url::class/:attachment/missing_u:style.jpg
验证附件内容类型:徽标,内容类型:%r{\Aimage/*\Z}
别名\u方法:头像,:徽标
_和_属于_许多流派吗
有很多:反馈请求
有许多:付款类型,如::收款人
有很多:链接,如::可链接,依赖::销毁
有很多:发行版
有很多:标签艺术家
已发送多条消息,如::发件人,类名:“消息”
有很多:信息,通过::对话
有很多:对话
有很多:歌曲,通过::反馈请求,来源::歌曲
有很多:艺人联系人,通过::歌曲,来源::艺人
有很多:合同,通过::对话
接受以下类型的\u嵌套\u属性\u:payment\u
接受链接的嵌套属性,如果:->(属性){attributes[:url]。blank?}则拒绝
验证:name、:real_name、presence:true
验证:城市,:国家/地区代码,:个人资料,状态:true,on::update
委托人:姓名,收件人::国家,前缀:true,允许\无:true
搜索范围:搜索,
反对::name,
关联_对:{genres::name},
使用:{
t搜索:{词典::英语}
}
pg搜索范围:根据相关性,
关联_对:{genres::name},
使用:{
搜索:{字典::英语,任何单词:true}
}
def自行批准
其中(“批准的地址不为空”)
结束
def批准?
现在批准了吗?
结束
def付款类型
付款类型。订单(“创建日期”)。最后
结束
def显示单元名称
名称
结束
def国家
国家/地区。新(代码:国家/地区代码)
结束
def位置
[城市、国家名称].compact.join(“,”)
结束
def徽标_url(样式=:中等)
logo.url(样式)
结束
#该品牌获得的利润(扣除Sendemo的利润)
def余额净额
((原始余额至i/100.0)*(1-付款。发送费用分数))。如果存在原始余额,则第(2)轮?
结束
#标签的余额是通过标签收取的全部余额,包括Sendemo的费用。
#这是为了向后兼容,应该更改
def余额=(新的_余额)
写入属性(:balance,(new\u balance.to\u f*100)。to\u i)如果存在new\u balance?
结束
def平衡
(原始余额至i/100.0)。如果存在原始余额,则舍入(2)?
结束
def原始余额
read_属性(:balance)
结束
def未读邮件数
messages.unread.count
结束
def未读邮件?
未读邮件数>0
结束
结束
值得一提的是,当我试图在rails控制台中编写代码以查看我的当前标签是否具有标签模型中提到的任何其他属性时,答案总是肯定或否定。引起错误的唯一问题是在检查@label.links时

例如:

>> @label.conversations
=> #<ActiveRecord::Associations::CollectionProxy []>

>> @label.name
=> "Test Label"

>> @label.links
!! #<NoMethodError: undefined method `extensions' for #<Hash:0x000000001001f1e8>
Did you mean?  extend>
>@label.conversations
=> #
>>@label.name
=>“测试标签”
>>@label.links

!! # 我也有类似的问题,我的问题是:

class Foo
当我在Foo上调用
.bar
时,我得到了
undefined\u方法:extensions
错误

我的解决方案是在Rails 6迁移期间将
->{uniq}
更改为
->{distinct}


希望这对某人有所帮助。

解决方案毕竟很简单。rails 5.2和rails 6.0之间的范围界定部分似乎发生了变化

Rails 5.2仍然支持一种在Rails 3中逐渐被弃用的语法,而Rails 6.0不再支持这种语法

在link.rb(我的链接模型)中,我定义范围的第15行出现了一个问题:

  default_scope { {order: ordered_links} }
适用于我的新语法如下:

  default_scope { order(ordered_links) }

信息取自

Koryto先生的答案看起来并不相同-当设置默认的
顺序时,另一个默认的
在哪里
。也许你应该试试

default_scope { order(ordered_links) }

您应该更改
Label::PersonalInformationsController
的定义以使用显式嵌套,因为使用范围解析运算符将导致意外的常量查找。我并不是说这是你当前困境的原因,但这是一堆等待发生的错误。嘿,麦克斯。谢谢你的建议。我现在在我的整个应用程序中根据您所附的样式指南进行更改。如果您使用的是一个名为的cop,它可以帮助您解决此问题。有相同的问题,但与HABTM关联。已经追踪到associations.rb:
extensions |=reflection.scope_for(klass.unscoped,owner)。extensions
Done@MyKoryto-mine是类似的症状,但原因不同。我相信这对许多用户都很有用。刚刚发布了我的问题的答案,因为我一小时前刚刚解决了它。您知道rails文档中是否有任何部分指出了我们遇到的问题?我已经搜索了一段时间,但运气不好。它没有改变我的应用程序中的任何东西,但它在SQL方面更有意义。我也会在我的标记答案上更新它。
  default_scope { order(ordered_links) }
default_scope { order(ordered_links) }