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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:03:28+00:00 2026-05-15T23:03:28+00:00

I have User, Game and GameView. GameView describe what games users have seen. Trouble

  • 0

I have User, Game and GameView. GameView describe what games users have seen. Trouble is I can’t figure out what conditions should I use to fetch unviewed games.

class User < ActiveRecord::Base
  has_many :game_views
  has_many :unviewed_games, :through => :game_views, :source => :game, ???what conditions???
end

class GameView < ActiveRecord::Base
  belongs_to :user
  belongs_to :game
end
  • 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-15T23:03:29+00:00Added an answer on May 15, 2026 at 11:03 pm

    I don’t see this as an association because an association is generally something where you have a foreign key pointing to something, but in this case you have no foreign key. I see this as more of an instance attribute which I would do like this:

    def unviewed_games
      Game.all(:conditions => ["id NOT IN (SELECT game_id FROM game_views WHERE user_id = ?", self.id])
    end
    

    You could do a NOT IN (1,2,3) by querying the viewed games, but that can get very inefficient, very very fast. This is one time I would write out the SQL. I would also do one more thing:

    def unviewed_games
      return @unviewed_games if defined(@unviewed_games)
      @unviewed_games = Game.all(:conditions => ["id NOT IN (SELECT game_id FROM game_views WHERE user_id = ?", self.id])
    end
    

    That will store it in an instance variable for the length of the request, and save you the multiple database hits. You can do ||=, but if somehow you were to get a nil, then you would still query the database multiple times. Rails should cache, but call me paranoid.

    Hope this helps!

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

Sidebar

Related Questions

Imagine that HTML page is a game surface (see picture). User can have n
I have created a game where the user can upload their high score and
In the case of a game where user have to click/use keys mutli and
I have a table which records users's scores at a game (a user may
If you have a user base of a million users for a game but
I have a game where user controls a character (with his finger) and i
I am making a game, and have a Game model and a User model.
i have wpf application, a game of sudoku. I have a model and user
I want to do mapping that does this: User can own multiple Games .
I have a User mapping of of a class in my system, these Users

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.