Testing associations with alias_attribute

I plan to use alias attributes for several of my model associations. Note: I fully know that I can also use this association through:

belongs_to :type, class_name: "AlbumType"

but I would like to continue studying the alias_attribute method . With that in mind, I have Albumone that belongs AlbumType.

class Album < ApplicationRecord
  alias_attribute :type, :album_type
  belongs_to :album_type
end

class AlbumType < ApplicationRecord
  has_many :albums
end

So far so good. Now I would like to test the aliased association in my album. It seems that the traditional belongs_tomusta-matcher is not smart enough to identify this type of album_type, even after specifying the class name. Of course, I am not against writing the traditional RSpec test, but I'm not entirely sure, as in this case. Any help would be greatly appreciated.

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
+4
1

alias_attribute . , shoulda ActiveRecord::Reflection . , alias_attribute, - getter, setter '?' . ActiveRecord, .

, alias_attribute ActiveRecord, shoulda .

. , , , ActiveRecord , . , a belongs_to :

build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})

alias_attribute album.build_album_type - , , .

- , , , belongs_to .

+1

Source: https://habr.com/ru/post/1656543/


All Articles