Add a list_by! macro

This commit is contained in:
Bat 2018-06-20 19:23:54 +01:00
parent e074af57ff
commit ddd840d31d
1 changed files with 12 additions and 0 deletions

View File

@ -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<Self> {
$table::table
$(.filter($table::$col.eq($col)))+
.load::<Self>(conn)
.expect("Error loading $table by $col")
}
};
}
macro_rules! get { macro_rules! get {
($table:ident) => { ($table:ident) => {
pub fn get(conn: &PgConnection, id: i32) -> Option<Self> { pub fn get(conn: &PgConnection, id: i32) -> Option<Self> {