[RAILS] How to make a factory with a model with polymorphic association

It's almost for myself, so I'll write it briefly.

You can comment on user and project.

comment.rb


belongs_to :commentable, polymorphic: true
belongs_to :commenter, class_name: 'User'

user.rb


has_many :comments, as: :commentable, dependent: :destroy

preject.rb


has_many :comments, as: :commentable, dependent: :destroy

Just write both factories

factories/comment.rb



FactoryBot.define do
    factory :project_comment, class: 'Comment' do
      association :commenter,   factory: :user
      association :commentable, factory: :project
      commentable_type { 'User' }
      comment          { 'Sample Comment' }
    end

    factory :user_comment, class: 'Comment' do
      association :commenter,   factory: :user
      association :commentable, factory: :user
      commentable_type { 'User }
      comment          { 'Sample Comment' }
    end

end

let(:user_comment) { FactoryBot.create(:user_comment) }

Can be called with.

Polymorphic related should be easy to add related models, Therefore, I think it should be easy to add a factory. If you write like this, you can increase the comment_type even if the number of related models increases in the future.

factories/comment.rb


FactoryBot.define do
  comment_type = [:user, :project]

  comment_type.each do |type|
    factory :"#{type}_comment", class: 'Comment' do
      association :commenter,   factory: :user
      association :commentable, factory: type
      commentable_type { type.to_s.camelize }
      comment          { 'Sample Comment' }
    end
  end
end

Conversely, if increasing comment_type does not support it, it should not be polymorphic. If the method and factory configurations change depending on the related model, why not consider STI instead of polymorphic.

Recommended Posts

How to make a factory with a model with polymorphic association
How to make a Java container
How to make a JDBC driver
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to make a Java array
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to make a jar file with no dependencies in Maven
How to make a Discord bot (Java)
How to rename a model with foreign key constraints in Rails
How to make an app with a plugin mechanism [C # and Java]
How to make a lightweight JRE for distribution
How to create member variables with JPA Model
How to make a follow function in Rails
How to make shaded-jar
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make batch processing with Rails + Heroku configuration
How to make LINE messaging function made with Ruby
How to delete a new_record object built with Rails
How to make an almost static page with rails
How to make JavaScript work on a specific page
How to manually generate a JWT with Rails Knock
How to delete custom Adapter elements using a custom model
[How to insert a video in haml with Rails]
How to make Laravel faster with Docker for Mac
How to make a cache without thinking too much
How to make a mod for Slay the Spire
How to get started with creating a Rails app
[Java] How to start a new line with StringBuilder
Java --How to make JTable
How to leave a comment
How to number (number) with html.erb
How to update with activerecord-import
[Rails] How to make seed
How to insert a video
How to create a method
How to take a screenshot with the Android Studio emulator
How to request a CSV file as JSON with jMeter
Let's write how to make API with SpringBoot + Docker from 0
How to use trained model of tensorflow2.0 with Kotlin / Java
How to divide a two-dimensional array into four with ruby
How to use a foreign key with FactoryBot ~ Another solution
[Beginner] Try to make a simple RPG game with Java ①
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
How to test a private method with RSpec for yourself
How to move another class with a button action of another class.
How to make an app using Tensorflow with Android Studio
Mapping to a class with a value object in How to MyBatis
How to set up a proxy with authentication in Feign
How to register as a customer with Square using Tomcat
How to scroll horizontally with ScrollView
Make a digging maze with Ruby2D
How to add columns to a table
I want to make a button with a line break with link_to [Note]
How to get started with slim
Try to make a simple callback
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Make a slideshow tool with JavaFX