From ddd840d31d1bf5eb39950754ad254b90a636326d Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 20 Jun 2018 19:23:54 +0100 Subject: [PATCH] Add a list_by! macro --- src/models/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/models/mod.rs b/src/models/mod.rs index 13edc569..fa4a4c82 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -12,6 +12,18 @@ macro_rules! find_by { }; } +macro_rules! list_by { + ($table:ident, $fn:ident, $($col:ident as $type:ident),+) => { + /// Try to find a $table with a given $col + pub fn $fn(conn: &PgConnection, $($col: $type),+) -> Vec { + $table::table + $(.filter($table::$col.eq($col)))+ + .load::(conn) + .expect("Error loading $table by $col") + } + }; +} + macro_rules! get { ($table:ident) => { pub fn get(conn: &PgConnection, id: i32) -> Option {