Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 560337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:19:57+00:00 2026-05-13T12:19:57+00:00

I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships

  • 0

I have 4 models, Message, Group, User, Membership

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, :through => :memberships
  has_many :groups_messages
  has_many :messages, :through => :groups_messages, :order => "created_at desc"

  named_scope :with_memberships, :include => :memberships

end

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships
end

class Message < ActiveRecord::Base
  has_and_belongs_to_many :recipients, :class_name => "User"
  has_many :groups_messages
  has_many :groups, :through => :groups_messages

  def accessible
   self.groups.with_memberships.map(&:user_ids).include?(User.current.id)
  end

end

Message can be posted to a Group, and if current User is a member of the group, he has a right to read it.

I am trying to check whether user is a member of the group with Message.accessible, but it produces one query to much:

 Group Load (0.1ms)   SELECT `groups`.* FROM `groups` INNER JOIN `groups_messages` ON `groups`.id = `groups_messages`.group_id WHERE ((`groups_messages`.message_id = 381)) AND ((`groups_messages`.message_id = 381)) 
 Membership Load (0.1ms)   SELECT `memberships`.* FROM `memberships` WHERE (`memberships`.group_id = 1) 
 User Load (0.1ms)   SELECT `users`.id FROM `users` INNER JOIN `memberships` ON `users`.id = `memberships`.user_id WHERE ((`memberships`.group_id = 1)) 

I don’t need User Load query – user_id is contained in Membership, so last query is useless.

I tried changing accessible method to

 def accessible
   self.groups.with_memberships.exists?(:user_id=>User.current.id)
 end

But then it tries to use user_id in Group Load query and fails of course.
How can i get rid of the last query?
Rails 2.3.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:19:57+00:00Added an answer on May 13, 2026 at 12:19 pm

    after understanding rails better, i realized that it was happening because of map(&:user_ids) in

    def accessible
      self.groups.with_memberships.map(&:user_ids).include?(User.current.id)
    end
    

    this behaviour was absolutely normal, my mistake was that i expected it to behave like groups.membership_user_ids, and it was behaving as groups.user_ids

    fixed by changing to

    def accessible
     Membership.exists?(["user_id=? and group_id in (?)",User.current.id,self.group_ids])
    end
    

    actually the part include?(User.current.id) wasn’t even working for some reason.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have the models User and Group, and the join table Membership which uses
I have a two models, Group and User User belongs_to Group and Group has_many
I have Messages and User models with corresponding tables. The Messages table has such
I have a User and Group model. User has_many Groups and Group belongs_to User
Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions
class Person has_many :owned_groups, :class_name => Group, :foreign_key => :owner_id has_many :owned_group_memberships, :through =>
i want to create a messenger and i have those models: Account Message Message
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.