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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:27:08+00:00 2026-05-26T01:27:08+00:00

I have a situation where I’m getting a bit stuck. A Post can have

  • 0

I have a situation where I’m getting a bit stuck.

A Post can have a User (as an author), and a Post can also have many Users (as a post can have other users tagged in it).

In my Post model:

belongs_to :user
has_and_belongs_to_many :users

In my User model:

has_and_belongs_to_many :posts

It works fine generally, but is getting rather stuck when I want to include users on my posts query, and sort the posts by the author’s name.

What should the correct design be? Perhaps replace user_id in Post with author_id?

  • 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-26T01:27:09+00:00Added an answer on May 26, 2026 at 1:27 am

    I can imagine User and Users messing up your joins. But mind you, as far as my experience this is something that rails does not solve well. Just renaming it author (whilst improving the readability/expressing the intention better) will not help, since it will still point to the same table users.

    When doing multiple joins with rails, it is very hard to express your conditions correctly. Rails creates aliases for your tables on the fly, unfortunately I have not find a way to address these aliases in my conditions.

    So I think you will either have to resort to a handcrafted sql (which will be fast) or use rails, and get the posts sorted on author-name first, and then retrieve the list of tagged users per post second (which will be slower, but pure ruby).

    I would go for the second option first, and optimize to the first if needed.

    [UPDATE: add a scope definition]

    Personally, I do not favor default_scope and instead would propose to define an explicit scope

    scope :with_authors_sorted, lambda { 
      Post.includes(:type).includes(:user).
           order('year DESC, week DESC, users.display_name ASC, posts.created_at DESC') 
    }
    

    which you can then just use in your controller.

    @posts = Post.with_authors_sorted
    

    Hope this helps.

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

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I have situation like this: user submits form with action='/pay' in '/pay' I have
We have a situation where users are allowed to upload content, and then separately
I have situation where I need to make sure that user has completed certain
Current situation: Have a timesheet that allows the user to enter their leave, TOIL,
I have a situation where I need to have a user select a region
is there any limit of POST arguments? I have situation where on dev server
I have a situation where I want something to be displayed when a user
I can't make git push origin B. I have situation something like this _____________________________________
i have situation like this: class IData { virtual void get() = 0; virtual

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.