Home Ruby on rails - how to join 4 tables?
Reply: 0

Ruby on rails - how to join 4 tables?

user674
1#
user674 Published in May 22, 2018, 12:09 am

This is what I have:

class Membership < ApplicationRecord
  belongs_to :user
  belongs_to :group
end

class User < ApplicationRecord
  has_one :user_score
  has_many :memberships
  has_and_belongs_to_many :groups, through: :memberships # an user can be owner or member of a group
end

class Group < ApplicationRecord
  has_many :memberships
  has_many :members, through: :memberships, source: :user
end

class UserScore < ApplicationRecord
  belongs_to :user
end

What I'm trying to do is to get a list of users from a group ordered by 'total_score' (which is an attribute of UserScore) but I've failing so far.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308898 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO