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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:09:11+00:00 2026-06-13T01:09:11+00:00

My application models a writing competition, with Users , Stories , Competitions and Submissions

  • 0

My application models a writing competition, with Users, Stories, Competitions and Submissions.

Users can participate in competitions through submissions, or as owners. I’d like one query or method that returns all competitions owned or participated in by a user. I’ve got a method returning those as an array, competitions | owned_competitions, but I’d really like to keep them ActiveRecord entities so I can chain, apply scopes, etc.

Is there a way to do that, or a better way to setup these relationships?

My models look like this:

user.rb:

has_many :stories
has_many :submissions
has_many :competitions, :through => :submissions
has_many :owned_competitions, :class_name => "Competition"

story.rb:

belongs_to :user
has_many :submissions
has_many :competitions, :through => :submissions

competition.rb:

belongs_to :owner, :class_name => "User", :foreign_key => 'user_id'
has_many :submissions
has_many :competitors, :through => :submissions, :source => :user
has_many :stories, :through => :submissions

scope :expiring_today, lambda { where("deadline = ?", Date.today) }

submission.rb:

belongs_to :user
belongs_to :story
belongs_to :competition
  • 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-13T01:09:12+00:00Added an answer on June 13, 2026 at 1:09 am

    What you want is absolutely possible. Actually, it’s probably possible without even writing any SQL, but I don’t know precisely how.

    Here’s what I’ve come up with:

    Competition.joins('left join submissions on submissions.competition_id = competitions.id').where('competitions.user_id = :uid OR submissions.user_id = :uid', :uid => user)
    

    We use a left join (rather than the default inner join) so that we can still return competitions that a user owns but which have no submissions yet.

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

Sidebar

Related Questions

I'm writing a web application with Django where users can upload files with statistical
I'm writing an application for the iPhone that lets users create 3D models which
I am writing an Android application which uses several 3D models. Such a model
My application is SL2 reading and writing data through an Entity Framework Model exposed
Currently my application caches models in memcache like this: memcache.set(somekey, aModel) But Nicks' post
My application structure is like this: application modules default student controllers forms studentParent.php models
I'm writing an application that models train routes, which are stored in the database
I'm currently writing unit tests for my Ruby on Rails application, and one of
I am writing a Django application that will track changes to the models, in
I am writing ruby on rails application, which will have 2 different users types

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.