Scala - calling foreach on a Seq to populate sample data

I just saw the following Scala source code in a Play Framework 2 sample application, and it struck me as a nice example of how to call the foreach method on a Seq to populate some sample data:

Seq(
  User("guillaume@sample.com", "Guillaume Bort", "secret"),
  User("maxime@sample.com", "Maxime Dantec", "secret"),
  User("sadek@sample.com", "Sadek Drobi", "secret"),
  User("erwan@sample.com", "Erwan Loisant", "secret")
).foreach(User.create)

As you can see on the last line, this code calls the create method on a User object, and that method looks like this:

def create(user: User): User = {
  DB.withConnection { implicit connection =>
    SQL(
      """
        insert into user values (
          {email}, {name}, {password}
        )
      """
    ).on(
      'email -> user.email,
      'name -> user.name,
      'password -> user.password
    ).executeUpdate()
    
    user
    
  }
}

Nothing too Earth-shattering, just a clean example of how to use a Seq with foreach to populate some sample data in Scala. This simple technique can be very useful when writing unit tests.

To see more of this code, check out the Global.scala and User.scala files in the Zentasks project in the Play Framework 2.1 distribution.