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 5961903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:01:52+00:00 2026-05-22T19:01:52+00:00

class Person has_many :owned_groups, :class_name => Group, :foreign_key => :owner_id has_many :owned_group_memberships, :through =>

  • 0
class Person
  has_many :owned_groups, :class_name => "Group", :foreign_key => :owner_id
  has_many :owned_group_memberships, :through => :owned_groups, 
              :source => :group_memberships 

  has_many :group_memberships, :foreign_key => "member_id"
  has_many :groups, :through => :group_memberships

end

class GroupMembership
  belongs_to :member, :class_name => 'Person'
  belongs_to :group
end

class Group
  belongs_to :owner, :class_name => "Person"
  has_many :group_memberships
  has_many :members, :through => :group_memberships
end

I want to create the model Message so a Person can post a new message in the group#show page. But for that she/he must be from the Group in question, or the owner, or have a group_membership (be a member) from this group.

How would be the associations of the model Message?

  • 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-22T19:01:53+00:00Added an answer on May 22, 2026 at 7:01 pm
    class Message < ActiveRecord::Base
      belongs_to :person
      belongs_to :group
      validate :has_group_permission
    
      def has_group_permission
        unless self.person.owned_groups.include?(self.group) || self.person.groups.include?(self.group)
          self.errors.add(:base, "you don't have permission to add a message to this group."
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: class Person < ActiveRecord::Base has_many :people_phones has_many :phones, :through => :people_phones end
class Party < ActiveRecord::Base belongs_to :hostess, class_name: 'Person', foreign_key: 'hostess_id' validates_presence_of :hostess end class
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have these models: Person has_many :messages_form_person, :foreign_key => :from_user_id, :class_name => :messages has_many
I have the following: class Person < ActiveRecord::Base has_many :tasks end class Task <
Consider the following polymorphic relationship class Person has_many :photos, :as => :attachable end class
Consider a simple association... class Person has_many :friends end class Friend belongs_to :person end
Assume a standard has_many :through relationship among three models class Person < ActiveRecord::Base has_many
Here is some code: class Person def initialize(age) @age = age end def age
If I have a Model like the following example class Person < ActiveRecord::Base has_many

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.