, . @jordanpg, , . , , , , , , :
, :
class User < ApplicationRecord
has_many :stories
has_many :comments
end
class Story < ApplicationRecord
belongs_to :user
has_many :comments, as: :commentable
end
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
end
factory :
FactoryBot.define do
factory :user do
sequence(:email) {|n| "person#{n}@exmaple.com"}
sequence(:slug) {|n| "person#{n}"}
end
factory :post do
body "this is the post body"
user
end
factory :comment do
body "this is a comment on a post"
user
association :commentable, factory: :post
end
end
, factory_bot , . : http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md#Associations
, , :
factory :comment_on_post, class: Comment do
body "this is a comment on a post"
user
association :commentable, factory: :post
end
factory :comment_on_comment, class: Comment do
body "this is a comment on a comment"
user
association :commentable, factory: :comment_on_post
end