小编Dan*_*rez的帖子

ActiveRecord左外连接和子句

我正在使用Rails 3和ActiveRecord,我不能让它生成我想要的查询而不插入几乎普通的SQL.

我只是想执行这个SQL查询(实际上查询有点复杂,但实际上这部分我无法正确).

SELECT DISTINCT users.*, possible_dates.*
FROM users 
LEFT OUTER JOIN possible_dates 
ON possible_dates.user_id = users.id 
AND possible_dates.event_id = MY_EVENT_ID;
Run Code Online (Sandbox Code Playgroud)

我设法使用

User.includes(:possible_dates)
    .joins("LEFT OUTER JOIN possible_dates
            ON possible_dates.user_id = users.id
            AND possible_dates.event_id = #{ActiveRecord::Base.sanitize(self.id)}"
    )
    .uniq
Run Code Online (Sandbox Code Playgroud)

但我觉得我遗漏了一些东西,只需AND使用ActiveRecord查询方法添加左连接的条件.我也尝试过这样的事情

User.includes(:possible_dates)
    .where('possible_dates.event_id' => self.id)
    .uniq
Run Code Online (Sandbox Code Playgroud)

但是这会产生(如预期的那样),一个WHERE在末尾有一个子句的查询,而不是AND我想要在连接上的子句.

顺便说一句,self上面的两个片段是我Event班级的一个实例.

谢谢您的帮助.

sql activerecord ruby-on-rails

8
推荐指数
1
解决办法
1989
查看次数

Akka远程actor,超类没有默认构造函数

我正在尝试使用akka远程actor发送消息,其中case类是在其构造函数中获取参数的超类的子类.

以下是重现问题的最小示例:

package com.tuvistavie.testremote

import akka.actor.{ Actor, ActorSystem, Props, ActorLogging }
import com.typesafe.config.ConfigFactory

abstract class Foo(val a: Int)
case class MessageFoo(override val a: Int) extends Foo(a)

object Sender {
  def main(args: Array[String]) {
    val system = ActorSystem("Sender", ConfigFactory.load.getConfig("sender"))
    val actor = system.actorFor("akka://Receiver@127.0.0.1:2552/user/receiver")
    actor ! MessageFoo(1)
  }
}

object Receiver {
  class ReceiverActor extends Actor with ActorLogging {
    def receive = {
      case m: MessageFoo => log.debug(m.toString)
    }
  }

  def main(args: Array[String]) {
    val system = ActorSystem("Receiver", ConfigFactory.load.getConfig("receiver"))
    val actor = …
Run Code Online (Sandbox Code Playgroud)

scala remote-actors actor akka

6
推荐指数
2
解决办法
1238
查看次数

Scala specs2嵌入了上下文

我正在尝试使用specs2在Scala中运行一些测试,但是我遇到了一些未执行的测试用例的问题.

这是一个简单的例子来说明我的问题.

BaseSpec.scala

package foo

import org.specs2.mutable._

trait BaseSpec extends Specification {
  println("global init")
  trait BeforeAfterScope extends BeforeAfter {
    def before = println("before")
    def after = println("after")
  }
}
Run Code Online (Sandbox Code Playgroud)

FooSpec.scala

package foo

import org.specs2.mutable._

class FooSpec extends BaseSpec {
  "foo" should {
    "run specs" in new BeforeAfterScope {
      "should fail" in {
        true must beFalse
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望测试失败,但似乎嵌套in语句中的"应该失败"的情况不会被执行.

如果我删除嵌套in语句或者BeforeAfterScope,测试行为正确,所以我想我错过了一些东西,但我没有设法在文档中找到它.

[编辑]

在我的用例中,我目前正在填充before方法中的数据库并在方法中清理它after.但是,我希望能够在没有清理的情况下拥有多个测试用例,并在每个测试用例之间重新填充数据库.这样做的正确方法是什么?

scala specs2

2
推荐指数
1
解决办法
2280
查看次数

标签 统计

scala ×2

activerecord ×1

actor ×1

akka ×1

remote-actors ×1

ruby-on-rails ×1

specs2 ×1

sql ×1