postgresql - Self joins in rails through array field on other model -
we have 2 postgresql tables follows:
keywords table: id, keyword (string) joins table: id, keyword_ids (integers array)
now, want add associations both keywords , aliases model follows
class keyword < activerecord::base has_many :joins end class join < activerecord::base belongs_to :keywords foreign_key: 'keyword_ids' end
how can in rails 4 , postgresql 9.3?
it seems can't done rails associations, did following model/class method
def alias_keywords aliaskeyword.select("keyword").joins("join alias_joins on alias_keywords.id = any(alias_joins.joins)").where("? = any(alias_joins.joins)", self.id).map{|x| x.keyword} end
now, can easily, access aliaskeywords.first.alias_keywords
Comments
Post a Comment