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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:15:36+00:00 2026-05-31T07:15:36+00:00

Sorry for the confusing title, I couldnt explain it better. Feel free to edit

  • 0

Sorry for the confusing title, I couldnt explain it better. Feel free to edit it.

I have this structure:

class Home < ActiveRecord::Base
 has_many :reservations

end

class User < ActiveRecord::Base
 has_many :reservations

end

class Reservation < ActiveRecord::Base
 belongs_to :user
 belongs_to :home
end

Which means that Reservations ar associated to the user that makes the reservation, and the
home that he is booking.

I am creating the reservations instances like this:

user_instance.reservations.create(:home => home_instance, :dates_and_other_data => ...)

Using this method, the reservation is properly created in the database, and I can retrieve it (with the rest of the reservations the user has made) with

user_instance.reservations

But, if I try to retrieve it from the home instance I used as a parameter, like this:

home_instance.reservations 

This returns an empty array. The opposite happens as well (I cannot retrieve the reservation from the user instance if I create it from the homes).

What am I doing wrong?

Aren´t these two sentences the same?:

Reservation.create(:user => user, :home => home, :other_stuff)
User.reservations.create(:home => home, :other_stuff)
  • 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-31T07:15:37+00:00Added an answer on May 31, 2026 at 7:15 am

    The user model has no way of knowing that you just created a reservation from the home model, and vice versa. When you access an association for the first time, ActiveRecord memoizes the result of that call, so it doesn’t need to call the database each time you subsequently use that association.

    home_instance.reservations   # This will pull from the databse, and store the
                                 # results in a variable.
    
    # This will create a new reservation record in the database.
    user_instance.reservations.create(:home => home_instance, :dates_and_other_data => ...)
    
    home_instance.reservations   # We don't go to the database here, since we already
                                 # have the stored variable. So we don't see the new
                                 # reservation.
    

    You can get around this by passing true as an argument to a has_many association, like so:

    home_instance.reservations(true)  # The true forces ActiveRecord to requery the DB>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everybody and sorry for the rather confusing question-title. I have a Hibernate Class
I'm sorry for the little confusing title but say I have a class with
sorry for the confusing title, its really hard for me to explain what i
Sorry if the question title is confusing. Let me explain further. I am building
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
Sorry for the confusing title. Let me explain via code: #include <string> #include <boost\function.hpp>
Sorry for the confusing title :P I'm sure I should know this but I
Sorry for the somewhat confusing title. Not sure really how to title this. My
Sorry if the title is confusing. I have several string expressions in an array
Sorry for the title may seem confusing. i have 2 element. with no relation

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.