我打算为几个模型关联使用别名属性。 注:我完全知道,我也别名通过这种关联:测试与alias_attribute的关联
belongs_to :type, class_name: "AlbumType"
但想进一步探索alias_attribute方法。考虑到这一点,我有一个Album
属于AlbumType
。
class Album < ApplicationRecord
alias_attribute :type, :album_type
belongs_to :album_type
end
class AlbumType < ApplicationRecord
has_many :albums
end
到目前为止好。我现在想在我的相册规范中测试别名关联。看起来,即使在指定了类名之后,传统的应该匹配器也不足以识别类型是album_type。对于编写传统的RSpec测试我当然不是不利的,但在这种情况下并不确定。任何帮助将非常感激。
RSpec.describe Album, type: :model do
describe "ActiveRecord associations" do
it { should belong_to(:album_type) }
context "alias attributes" do
it { should belong_to(:type).class_name("AlbumType") }
end
end
end
感谢您为我清理。目前正在设置我的ActiveModel序列化器的过程中,所以这是一个时间。 –