Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count rows in Lift (Scala's web framework)

Tags:

scala

lift

I want to add a property to my User model that returns the number of rows in the Project table that have a user Id of the user.

So something like this...

def numProjects = {

    /* somehow get count from Project table
       The straight sql would be:
       SELECT COUNT(*) FROM projects WHERE userId = <the current user>

     */

}
like image 740
joe Avatar asked Sep 23 '08 02:09

joe


1 Answers

According to the documentation here (found here), assuming you're looking for the project count for a User of id 1234 and assuming that your Project model inherits the MetaMapper trait (probably through KeyedMetaMapper), it seems you can use the count method as such:

Project.count(By(User.id, 1234))

or

Project.count(BySql("userId = ?", 1234))

I can't test because I haven't used Lift yet, but it looks right... :) Let me know if it works!

like image 56
marzagao Avatar answered Sep 19 '22 22:09

marzagao