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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:48:30+00:00 2026-06-11T18:48:30+00:00

I’m trying setup a Rails app that will be something like a game. The

  • 0

I’m trying setup a Rails app that will be something like a game. The app has Users, each of which have Pawns that they can create. A User can search other users and the Pawns that they created, and challenge another one if they like, using one of their own Pawns. The challenged user can then accept/decline the challenge.

Right now I can add/delete Pawns for a User fine, and my models look like this:

class User < ActiveRecord::Base
  has_many :pawns, dependent: :destroy

and

class Pawn < ActiveRecord::Base
  belongs_to :user

Now, if User1 wants to challenge a Pawn created by User2, he looks at User2’s list of Pawns and clicks a “Challenge” button for the Pawn he wants. User1 then has to select one of his Pawns to use for the challenge and clicks save. Now User2 needs to either accept/decline the challenge.

I’m having a hard time wrapping my head around how the challenges should be setup. My thought is that each Pawn will have a self-referential many-to-many relationship, almost like a friendship relationship would be setup. However, I don’t know if I should consider the challenge something related to the User or the Pawn.

Whats the best way to model something like this?

EDIT:

Here’s a diagram of what I’m trying to accomplish. I definitley think I need some sort of association setup. Result would hold statistics of that Pawn for that Challenge (something like time_spent, clicks_made, etc.). Challenge would also have a column for winner or something similar.

enter image description here

  • 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-06-11T18:48:31+00:00Added an answer on June 11, 2026 at 6:48 pm

    Your challenge may have association defined for each type of pawn.

    class Challenge < ActiveRecord::Base
      # attributes :challengee_id, :challenger_id
    
      belongs_to :challengee, class_name: "Pawn"
      belongs_to :challenger, class_name: "Pawn"
    
      has_many :results
    end
    

    Pawns will have associations for each type of challenge.

    class Pawn < ActiveRecord::Base
      # attributes :user_id
    
      belongs_to :user
    
      has_many :results
      has_many :initiated_challenges, class_name: "Challenge", foreign_key: :challenger_id
      has_many :received_challenges, class_name: "Challenge", foreign_key: :challengee_id
    end
    

    It’s probably ok to denormalize challenge_id for the result records.

    class Result < ActiveRecord::Base
      # attributes: pawn_id, challenge_id, :result
      belongs_to :pawn
      belongs_to :challenge
    end
    

    Your user can have associations to pawns and to challenges through pawns. A simple way to get both challenge types associated with a user would be to combine the results of the two challenge associations (initiated and received) into one method #challenge.

    class User < ActiveRecord::Base
      has_many :pawns
      has_many :initiated_challenges, through: :pawns, source: :initiated_challenges
      has_many :received_challenges, through: :pawns, source: :received_challenges
    
      def challenges
        initiated_challenges + received_challenges
      end
    end
    

    Performance optimizations for this method could include denormalizing the user_ids on to Challenge as :challengee_user_id and :challenger_user_id… or caching a list of challenge ids on the user so you make one query instead of two.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

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.