Thursday, 15 January 2015

sql - Adding eager loading to custom has_many relationships -



sql - Adding eager loading to custom has_many relationships -

groups , users have has_many through relationships on table user_roles, specifies role users have in each grouping (pending, member, admin, etc). given group, how might homecoming users matching particular role in grouping such code dry , eager loads appropriate associations?

class grouping < activerecord::base has_many :user_roles, dependent: :destroy has_many :users, through: :user_roles def members(role) self.users.includes(:user_roles).where("user_role.role = ?", role) # returns next error message: # pg::undefinedtable: error: missing from-clause entry table "user_role" end

your table name 'user_roles' , not 'user_role'. alter name of table in clause 'user_roles'.

def members(role) self.users.includes(:user_roles).where("user_roles.role = ?", role) end

sql ruby-on-rails activerecord

No comments:

Post a Comment