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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:18:24+00:00 2026-06-08T18:18:24+00:00

I have a has_many :through association setup between two tables (Post and Category). The

  • 0

I have a has_many :through association setup between two tables (Post and Category). The reason I’m using has_many :through instead of HABTM is that I want to do some validation on the join table (PostCategory).

So I have 4 models in use here:

User:

has_many :posts
has_many :categories

Post:

belongs_to :user
has_many :post_categories
has_many :categories, :through => :post_categories

Category:

belongs_to :user
has_many :post_categories
has_many :posts, :through => :post_categories

PostCategory:

belongs_to :post
belongs_to :category

Basically what I want is: Users can create posts, users can also create their own categories. A user can then categorize posts (not just their posts, any posts). A post can be categorized by many different users (in different ways potentially), and a category could contain many different posts (A user could categorize N posts under a specific category of theirs).

Here’s where it gets a little bit tricky for me (I’m a Rails noob).
A post can ONLY belong to ONE category for a given user. That is, a post CANNOT belong to more than ONE category for any user.

What I want to be able to do is create a validation for this. I haven’t been able to figure out how.
I’ve tried things like (inside PostCategory)

validates_uniqueness_of :post_id, :scope => :category_id

But I realize this isn’t correct. This would just make sure that a post belongs to 1 category, which means that after one user categorizes the post, no other user could.

Really what I’m looking for is how to validate this in my PostCategory model (or anywhere else for that matter). I’m also not against changing my db schema if that would make things easier (I just felt that this schema was pretty straight forward).

Any ideas?

  • 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-08T18:18:25+00:00Added an answer on June 8, 2026 at 6:18 pm

    Option 1 : use a before_save. In it, do a SQL look up to make sure a post with a similar category for your user doesn’t exist (take care that on edit, you’ll have to make sure you don’t look-up for the current Post that is already in the DB)

    Option 2 : custom validators :
    http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#custom-validators

    Never used them, but sounds like it can do what you want

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

Sidebar

Related Questions

I have a has_many through association setup between a song model and an artist
I'm trying to setup a simple has_many :through => association. I have 3 models.
I have Product model and it has many categories with a has_many :through association
I have the following :has_many, :through association for my models: Group Model: has_many :group_users
Is it possible to have multiple has_many :through relationships that pass through each other
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events
I have an Organization model that has_many users through affiliations. And, in the form
I have two models, Worker and Project, and they are connected with has_many through
I have a event model that has many invitations. Invitations are setup through checkboxes
I have a has_many :through association, and I am iterating over the children to

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.