NoMethodError: private method `y' in ActiveRecord when accessing an attribute
NoMethodError: private method `y' in ActiveRecord when accessing an attribute
我有一个模型 Artboard 和一个模型组,它们通过称为 ArtboardsGroups 的 table 具有多对多关系,它具有与关系相关的 x 和 y 值的属性
class Artboard < ApplicationRecord
has_many :artboards_groups, dependent: :destroy
has_many :groups, -> { select("groups.id as id, groups.name as name, artboards_groups.x as x, artboards_groups.y as y, artboards_groups.index as index, artboards_groups.r as r") }, through: :artboards_groups
end
class ArtboardsGroup < ApplicationRecord
belongs_to :artboard
belongs_to :group
end
class Group < ApplicationRecord
has_many :artboards_groups, dependent: :destroy
has_many :artboards, through: :artboards_group
end
当我尝试自己访问模型时,该模型工作得很好,但是当我尝试通过画板 select 分组并访问 'y' 属性时,我得到一个错误,它是私有的方法
NoMethodError: private method `y' called for #<Group id: 5, name: nil>
根据这个 thread (From over 10 years ago) it is because there is a private method in ActiveRecord::Base called 'y' /lib/ruby/2.5.0/psych/y.rb which is from a gem called psych 这是一个 yaml 解析器
我不想更改 'y' 的属性名称,因为它引用的是坐标系并且 (x,y) 是坐标的标准。还有其他方法可以解决这个问题吗?
class Group < ApplicationRecord
undef :y
has_many :artboards_groups, dependent: :destroy
has_many :artboards, through: :artboards_group
end
irb(main):001:0> g = Group.new(y: 1, x: 2)
=> #<Group id: nil, x: 2.0, y: 1.0, created_at: nil, updated_at: nil>
irb(main):002:0> g.y
=> 1.0
:y
方法很可能来自 yaml 解析器 psych which monkeypatches it into the Kernel class。
我有一个模型 Artboard 和一个模型组,它们通过称为 ArtboardsGroups 的 table 具有多对多关系,它具有与关系相关的 x 和 y 值的属性
class Artboard < ApplicationRecord
has_many :artboards_groups, dependent: :destroy
has_many :groups, -> { select("groups.id as id, groups.name as name, artboards_groups.x as x, artboards_groups.y as y, artboards_groups.index as index, artboards_groups.r as r") }, through: :artboards_groups
end
class ArtboardsGroup < ApplicationRecord
belongs_to :artboard
belongs_to :group
end
class Group < ApplicationRecord
has_many :artboards_groups, dependent: :destroy
has_many :artboards, through: :artboards_group
end
当我尝试自己访问模型时,该模型工作得很好,但是当我尝试通过画板 select 分组并访问 'y' 属性时,我得到一个错误,它是私有的方法
NoMethodError: private method `y' called for #<Group id: 5, name: nil>
根据这个 thread (From over 10 years ago) it is because there is a private method in ActiveRecord::Base called 'y' /lib/ruby/2.5.0/psych/y.rb which is from a gem called psych 这是一个 yaml 解析器
我不想更改 'y' 的属性名称,因为它引用的是坐标系并且 (x,y) 是坐标的标准。还有其他方法可以解决这个问题吗?
class Group < ApplicationRecord
undef :y
has_many :artboards_groups, dependent: :destroy
has_many :artboards, through: :artboards_group
end
irb(main):001:0> g = Group.new(y: 1, x: 2)
=> #<Group id: nil, x: 2.0, y: 1.0, created_at: nil, updated_at: nil>
irb(main):002:0> g.y
=> 1.0
:y
方法很可能来自 yaml 解析器 psych which monkeypatches it into the Kernel class。