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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:22:23+00:00 2026-05-13T18:22:23+00:00

Here’s the scoop: As a learning exercise, I’m trying to write a Rails clone

  • 0

Here’s the scoop:

As a learning exercise, I’m trying to write a Rails clone of one of the many location-based games out there (Foursquare, Gowalla, etc.) I have Users who create and check in to Stores.

In ActiveRecord terms:

:user has_many :stores
:store belongs_to :user

But now I’ve created a third model – Checkins. The table behind the model contains two fields, user_id (to record which User made the checkin), and store_id (to record what Store that User checked in to).

Once again, in AR terms:

:checkin belongs_to :user
:checkin belongs_to :store

:user has_many :checkins
:store has_many :checkins

This all works well and good – in my User and Store views, I can call @user.checkins and @store.checkins respectively. The only thing is, in this manner I can only retrieve the user_id or store_id, where I really want to get the username or store name. So I figure the intermediate checkin table lends itself perfectly to a usage of :through:

:user has_many :stores, :through => :checkins
:store has_many :users, :through => :checkins

It makes sense, but the thing is that a User already has_many Stores – the ones he’s created! And on his user page, I need to list both the Stores he’s created AND the ones he’s checked in to. I’m still trying to wrap my head around has_many_and_belongs_to so I’m not sure if that would set me in the right direction. Anyone care to offer a clue?

  • 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-13T18:22:23+00:00Added an answer on May 13, 2026 at 6:22 pm

    Rails makes it easy to handle this situation. One solution: your user can use a different relationship name for the second set of stores. For example:

    class Checkin
      belongs_to :store
      belongs_to :user
    end
    
    class Store
      belongs_to :user
      has_many :checkins
    end
    
    class User
      has_many :stores
      has_many :checkins
    
      has_many :visited_stores, :through => :checkins, :source => :store
    end
    

    Using the :source option tells ActiveRecord to look for the Checkin association :store when building the list of visited stores. Alternatively, you could’ve said

    has_many :created_stores, :class_name => "Store"
    has_many :stores, :through => :checkins
    

    In this case, you’re renaming the owned stores instead of the visited ones.

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

Sidebar

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.