Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何建立工厂女孩协会?_Ruby_Ruby On Rails 3_Factory Bot - Fatal编程技术网

Ruby 如何建立工厂女孩协会?

Ruby 如何建立工厂女孩协会?,ruby,ruby-on-rails-3,factory-bot,Ruby,Ruby On Rails 3,Factory Bot,我在建立协会方面遇到了困难。我的模型定义如下: class Conversation belongs_to :user1 belongs_to :user2 has_many :messages end 我定义了这些工厂 factory :user do name "name" end factory :female, parent: :user do gender 'f' end factory :male, parent: :user do gender 'm

我在建立协会方面遇到了困难。我的模型定义如下:

class Conversation
  belongs_to :user1
  belongs_to :user2
  has_many :messages
end
我定义了这些工厂

factory :user do
  name "name"
end

factory :female, parent: :user do
  gender 'f'
end 

factory :male, parent: :user do
  gender 'm'
end 

factory :message do
  message "message"
  conversation
end
现在,我正在尝试创建工厂“带有消息的对话”

factory :conversation do
    read false
    association :user1, factory: :male
    association :user2, factory: :female    
    factory :conversation_with_messages do

      ignore do
        messages_count 10
      end

      after(:create) do |conversation, evaluator|
        FactoryGirl.create_list(:message, evaluator.messages_count, author: conversation.user1)
      end
    end
  end
但是执行
FactoryGirl.create(:conversation\u with\u messages)
会出现数据库错误,说明user1\u id列需要不为null


我想知道为什么这个专栏没有被填满,我在这里做错了什么?

您在对话模型关系中指定了
类名了吗

class Conversation
  belongs_to :user1, class_name: 'User'
  belongs_to :user2, class_name: 'User'
  has_many :messages
end

您是否在对话模型关系中指定了
类名

class Conversation
  belongs_to :user1, class_name: 'User'
  belongs_to :user2, class_name: 'User'
  has_many :messages
end

当测试困难时,考虑修改您的设计。我突然想到两个想法:

1)必须
用户
s有多个
对话
s?

如果类似Twitter的直接消息模型(任意两个用户之间的一次连续对话)是可以接受的,那么您可以选择以下方式:

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User'
  belongs_to :recipient, class_name: 'User'

  default_scope order("created_at DESC")

  def read?
    !self.unread?
  end

  def read_or_unread
    self.unread? ? "unread" : "read"
  end
end

class User < ActiveRecord::Base
  has_many :messages, foreign_key: :recipient_id

  def messages_grouped_by_sender
    msg_ids = messages.select("MAX(id) AS id").group(:sender_id).collect(&:id)
    Message.includes(:sender).where(id: msg_ids)
  end
end

class Conversation

  THEM_TO_ME = "sender_id = :their_id AND recipient_id = :my_id"
  ME_TO_THEM = "sender_id = :my_id AND recipient_id = :their_id"

  def initialize(me, them)
    @me = me
    @them = them
  end

  def them
    @them
  end

  def thread
    Message.where("#{ME_TO_THEM} OR #{THEM_TO_ME}", ids)
  end

  def unread?
    # Checking only the newest message is good enough
    messages_to_me.first.try(:unread)
  end

  def mark_as_read
    messages_to_me.where(:unread => true).update_all(:unread => false)
  end

  def to_or_from_me(message)
    message.sender == @me ? "From" : "To"
  end

  private

  def messages_to_me
    Message.where(THEM_TO_ME, ids)
  end

  def ids
    { :my_id => @me.id, :their_id => @them.id }
  end
end

当测试困难时,考虑修改您的设计。我突然想到两个想法:

1)必须
用户
s有多个
对话
s?

如果类似Twitter的直接消息模型(任意两个用户之间的一次连续对话)是可以接受的,那么您可以选择以下方式:

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User'
  belongs_to :recipient, class_name: 'User'

  default_scope order("created_at DESC")

  def read?
    !self.unread?
  end

  def read_or_unread
    self.unread? ? "unread" : "read"
  end
end

class User < ActiveRecord::Base
  has_many :messages, foreign_key: :recipient_id

  def messages_grouped_by_sender
    msg_ids = messages.select("MAX(id) AS id").group(:sender_id).collect(&:id)
    Message.includes(:sender).where(id: msg_ids)
  end
end

class Conversation

  THEM_TO_ME = "sender_id = :their_id AND recipient_id = :my_id"
  ME_TO_THEM = "sender_id = :my_id AND recipient_id = :their_id"

  def initialize(me, them)
    @me = me
    @them = them
  end

  def them
    @them
  end

  def thread
    Message.where("#{ME_TO_THEM} OR #{THEM_TO_ME}", ids)
  end

  def unread?
    # Checking only the newest message is good enough
    messages_to_me.first.try(:unread)
  end

  def mark_as_read
    messages_to_me.where(:unread => true).update_all(:unread => false)
  end

  def to_or_from_me(message)
    message.sender == @me ? "From" : "To"
  end

  private

  def messages_to_me
    Message.where(THEM_TO_ME, ids)
  end

  def ids
    { :my_id => @me.id, :their_id => @them.id }
  end
end

出于某些原因,我需要分组对象来聚合消息,而测试本身并不困难-我只是在为我认为非常简单的用例创建合适的FactoryGirl代码时遇到了一个问题。出于某些原因,我需要分组对象来聚合消息,测试本身并不难——我只是在为我认为非常简单的用例创建合适的FactoryGirl代码时遇到了一个问题。