Ruby on rails 3 如何定义与factory girl/bot的多态关联
我有一个用户和故事模型,他们都有评论 我宣布以下模型如下:Ruby on rails 3 如何定义与factory girl/bot的多态关联,ruby-on-rails-3,factory-bot,Ruby On Rails 3,Factory Bot,我有一个用户和故事模型,他们都有评论 我宣布以下模型如下: class Comment belongs_to :commentable, polymorphic: true belongs_to :user end class User end class Story end 现在,我想用FactoryGirl声明一个comment对象,该对象属于同一个用户,即值得称赞的用户和用户 以下是我目前的代码: FactoryGirl.define do factory :user d
class Comment
belongs_to :commentable, polymorphic: true
belongs_to :user
end
class User
end
class Story
end
现在,我想用FactoryGirl声明一个comment对象,该对象属于同一个用户,即值得称赞的用户和用户
以下是我目前的代码:
FactoryGirl.define do
factory :user do
sequence(:email) {|n| "person#{n}@exmaple.com"}
sequence(:slug) {|n| "person#{n}"}
end
factory :comment do
occured_at { 5.hours.ago }
user
association :commentable, factory: :user
end
end
这里的问题是写评论的用户和值得称赞的用户不一样
我为什么要解决这个问题
许多TNX首先,我认为你还没有完全建立起你的协会。。。我想这就是你想要的:
class Comment < AR
belongs_to :commentable, polymorphic: true
end
class User < AR
has_many :comments, as: :commentable
end
class Story < AR
has_many :comments, as: :commentable
end
作为一种风格,模型名称的选择在这里有点混乱。用户如何“可评论”?如果你是指其他类型的文字,我会选择不同的名称。如果你指的是“用户档案”或类似的东西,也一样。我遇到这个问题是因为我个人有一个类似的问题,刚刚解决了它。像@jordanpg一样,我很好奇用户是如何评论的。如果我理解正确,问题在于撰写故事的用户和撰写故事评论的用户可能是不同的用户:
- 用户_1编写了一个故事
- 用户_2(或任何用户)可以评论用户_1的故事
# app/models/user.rb
class User < ApplicationRecord
has_many :stories
has_many :comments
end
# app/models/story.rb
class Story < ApplicationRecord
belongs_to :user
has_many :comments, as: :commentable
end
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
end
# spec/factories.rb
FactoryBot.define do
factory :user do
sequence(:email) {|n| "person#{n}@example.com"}
sequence(:slug) {|n| "person#{n}"}
end
factory :story do
body "this is the story body"
user
end
factory :comment do
body "this is a comment on a story"
user
association :commentable, factory: :story
end
end
factory :comment_on_story, class: Comment do
body "this is a comment on a story"
user
association :commentable, factory: :story
end
factory :comment_on_comment, class: Comment do
body "this is a comment on a comment"
user
association :commentable, factory: :comment_on_story
end
之所以这样做,部分原因是factory\u bot
将自动生成您正在创建的任何子项的父项。他们关于关联的文档非常好:
如果您需要用户能够对评论发表评论,您可以这样做:
# app/models/user.rb
class User < ApplicationRecord
has_many :stories
has_many :comments
end
# app/models/story.rb
class Story < ApplicationRecord
belongs_to :user
has_many :comments, as: :commentable
end
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
end
# spec/factories.rb
FactoryBot.define do
factory :user do
sequence(:email) {|n| "person#{n}@example.com"}
sequence(:slug) {|n| "person#{n}"}
end
factory :story do
body "this is the story body"
user
end
factory :comment do
body "this is a comment on a story"
user
association :commentable, factory: :story
end
end
factory :comment_on_story, class: Comment do
body "this is a comment on a story"
user
association :commentable, factory: :story
end
factory :comment_on_comment, class: Comment do
body "this is a comment on a comment"
user
association :commentable, factory: :comment_on_story
end
好的,但问题是,我如何将一个用户对象关联到两个属于关联的用户对象?我想在comment类中添加同样值得称赞的user-to-user属性。你也可以在这里找到这个问题的解决方案:我认为你的
factory:post
应该是factory:story
:facepalm:你完全正确。谢谢你指出这一点!我刚修好。