use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use models::{ comments::Comment, posts::Post }; use schema::mentions; #[derive(Queryable, Identifiable)] pub struct Mention { pub id: i32, pub mentioned_id: i32, pub post_id: Option, pub comment_id: Option } #[derive(Insertable)] #[table_name = "mentions"] pub struct NewMention { pub mentioned_id: i32, pub post_id: Option, pub comment_id: Option } impl Mention { insert!(mentions, NewMention); get!(mentions); list_by!(mentions, list_for_user, mentioned_id as i32); pub fn get_post(&self, conn: &PgConnection) -> Option { self.post_id.and_then(|id| Post::get(conn, id)) } pub fn get_comment(&self, conn: &PgConnection) -> Option { self.post_id.and_then(|id| Comment::get(conn, id)) } }