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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:45:47+00:00 2026-05-31T06:45:47+00:00

In the given case a User may: invite many invitees (has_many :invitations) accept one

  • 0

In the given case a User may:

  • invite many invitees (has_many :invitations)
  • accept one invitation (has_one :invitation)

According to http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association a has_many :through association should allow me to use a shortcut like the one in the following functional test.

However, it fails with the error noted in the comment.

snippet from functional test:

assert_difference('Invitation.count') do # WORKS
  post :create, :user => { :email => invitee_email, :password => "1password" }
end

@invitee = User.find_by_email(invitee_email)
@invitation = Invitation.find_by_issuer_id_and_invitee_id(@issuer.id, @invitee.id)
assert @invitation.valid? # WORKS
assert_present @invitation.issuer # WORKS
assert_present @invitation.invitee # WORKS

# TODO: repair
assert_present @issuer.invitees # FAILS with "[] is blank"
assert_present @invitee.issuer # FAILS with "nil is blank"

snippet from the method under test:

@issuer.create_invitation(:invitee => @invitee, :accepted_at => Time.now)
# tested as well - also fails the test:
# Invitation.create!(:issuer => @issuer, :invitee => @invitee, :accepted_at => Time.now)

relevant parts of invitation.rb:

belongs_to :issuer, :class_name => "User"
belongs_to :invitee, :class_name => "User"

validates_presence_of :issuer
validates_presence_of :invitee

relevant parts of user.rb:

has_many :invitations, :foreign_key => 'invitee_id', :dependent => :destroy
has_many :invitees, :through => :invitations
has_one :invitation, :foreign_key => 'issuer_id', :dependent => :destroy
has_one :issuer, :through => :invitation

Now I wonder:

  • What is the correct ‘shortcut’?
  • Are my models set up correctly in the first place?
  • 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-31T06:45:49+00:00Added an answer on May 31, 2026 at 6:45 am

    I guess you’re trying to design the logic like this:
    User 1 can invite user 2, user 3, then the data is:

    issuer_id, invitee_id
    1,         2
    1,         3
    

    Suppose user 2 accepted the invitation, the data become:

    issuer_id, invitee_id
    1,         2
    1,         3
    2,         1
    

    What if user 2 invites user 1 later? If my guess is right, your design wouldn’t work.

    I think you also have some misunderstanding about the associations. has_many :invitations, :foreign_key => 'invitee_id' means a user, as an invitee, received many invitations. And has_one :invitation, :foreign_key => 'issuer_id' means a user, as an issuer, sent an invitation.

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

Sidebar

Related Questions

Given a case where I have an object that may be in one or
Answerers: How does one literally detect the user's font scale, so that it may
Is there a CTRL+space -like way of auto-constructing a switch case around a given
Given the flexibility of HTML codes, parsing out paragraphs as seen by a user
I have some troubles when using javascript code for Facebook invite friends. Details: User
I've got a page of elements, each of which, depending on user interaction, may
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given a package, how can I automatically find all its sub-packages?
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given a date range how to calculate the number of weekends partially or wholly

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.