1

次のようなことを実行したいと思います。

最初にユーザーが「フォローしている」ユーザーで、次に追加のポイントスコアで並べ替えられたユーザーのリストを返したいと思います。私が書いた以下のコードは機能しません。なぜなら、資金提供者は持ち上げられた Slick タイプであり、リストに決して見つからないからです。

        //The following represents the query for only funders who we are following
        val following_funders:  List[User]  = (
            for {
          funder <- all_funders
          f <- follows if f.followerId === id //get all the current users follower objects
          if f.followeeId === funder.id
        } yield funder
        ).list

      val all_funders_sorted = for {
        funder <- all_funders
        following_funder =  following_funders contains funder
      } yield (funder, following_funder)
      //sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
      all_funders_sorted.sortBy(_._2.desc).sortBy(_._1.score.desc).map( x => x._1 )  

すべての助けに感謝します!

4

2 に答える 2

3

Slick では ID (主キー) を扱う必要があります。これが、データベース側でオブジェクトが一意に識別される方法です。最初のクエリを実行する必要はありません。in演算子を使用して最初に実行せずに、2 番目のコンポーネントとして使用できます。

    //The following represents the query for only funders who we are following
    val following_funders_ids = (
        for {
      funder <- all_funders
      f <- follows if f.followerId === id //get all the current users follower objects
      if f.followeeId === funder.id
    } yield funder.id

  val all_funders_sorted = for {
    funder <- all_funders
    following_funder = funder.id in following_funders_ids
  } yield (funder, following_funder)
  //sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
  all_funders_sorted.sortBy(_._1.impactPoints.desc).sortBy(_._2.desc).map( x => x._1 )   

最初に次の順序で並べ替えたい場合は、並べ替え順序が間違っていることに注意してください。Slick は、Scala コレクションがそのように機能するため、次のよう.sortBy(_.a).sortBy(_.b)に変換されます。ORDER BY B,A

scala> List( (1,"b"), (2,"a") ).sortBy(_._1).sortBy(_._2)
res0: List[(Int, String)] = List((2,a), (1,b))
于 2014-04-22T10:06:44.190 に答える
-1

「inSet」を使用して、次の方法でそれを理解することになりました

        //The following represents the query for only funders who we are following
        val following_funders_ids:  List[Long]  = (
            for {
          funder <- all_funders
          f <- follows if f.followerId === id //get all the current users follower objects
          if f.followeeId === funder.id
        } yield funder.id
        ).list
      val all_funders_sorted = for {
        funder <- all_funders
        following_funder = funder.id inSet following_funders_ids
      } yield (funder, following_funder)
      //sort the funders by whether or not they are following the funder and then map it to only the funders (i.e. remove the boolean)
      all_funders_sorted.sortBy(_._2.desc).sortBy(_._1.impactPoints.desc).map( x => x._1 )  
于 2014-04-22T01:21:57.097 に答える