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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:48:20+00:00 2026-06-14T06:48:20+00:00

I’m working on a three model relationship with one aspect that I’m not sure

  • 0

I’m working on a three model relationship with one aspect that I’m not sure how to approach. Here’s the basic relationship:

class Taxonomy
  has_many :terms
  # attribute: `inclusive`, default => false
end

class Term
  belongs_to :taxonomy
  has_and_belongs_to_many :photos
end

class Photo
  has_and_belongs_to_many :terms
end

This is pretty straightforward stuff except for one thing:

A Taxonomy can be either ‘Inclusive’ or ‘Exclusive’. Exclusive means the terms are mutually exclusive, Inclusive means they’re not.

Now, I can handle this on the client-side without a problem, but I am not quite sure how to set up a validation on Photos (or somewhere else) that says basically: “validate that no more than one term from an exclusive taxonomy is associated with this record.”

Bonus Question

Also, while it’s not essential, I’ve been thinking it would be nice to setup a join of some kind between Taxonomies and Photos, ie., I’d like an easy way to query for all the photos that have been classified with terms from a given taxonomy.

I think I can do this with something like Photo.where('term_id in ?', Taxonomy.find(1).terms.map(&:id)) but, obviously that’s not very elegant. I’m pretty sure has_many :through can’t work since terms are habtm to photos, so if anyone knows a more elegant way to setup that relationship I’d love to hear it.

Thanks!


Update, to further explain the taxonomy idea:

If a Taxonomy is exclusive ie. taxonomy.inclusive = false, then there can only be one term from that taxonomy attached to a given photo. Now, a Photo may have more than one taxonomy applied to it. For instance:

Taxonomy: Colors, Inclusive
Terms: Red (id 1), Blue (id 2), Green (id 3)

Taxonomy: Region, Exclusive
Terms: North America (id 4), South America (id 5)

So let’s say we have a photo with a lot of red and blue in it, so it gets those two terms, and it might be in South America, so it gets that term. But, a Photo can’t be of both North America and South America.

So in this case if we called: photo.terms.map &:id we would get [1,2,4]

On the client side I can basically do (pseudo code)

form_for photo
- Taxonomy.each do |tax|
  - if tax.inclusive?
    - tax.terms.each do |term|
      - check_box term
  - else
    - tax.terms.each do |term|
      - radio_button term

But on the model side, I would like to say, that a photo can have any of term_id 1,2,3, but only either/or 4 and 5.

Does that make sense?

  • 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-14T06:48:21+00:00Added an answer on June 14, 2026 at 6:48 am

    For your validation problem, you can add a uniqueness validation on Term model for taxonomy_id field which is only executed if the taxonomy is not inclusive, ie :

    class Term
      validates :taxonomy_id, uniqueness: true, unless: Proc.new { |term| term.taxonomy.inclusive }
    end
    

    You can also do this with a custom validation method :

    class Term
      validate :uniqueness_of_taxonomy_if_exclusive
    
      def uniqueness_of_taxonomy_if_exclusive
        if !taxonomy.inclusive && Term.find_by_taxonomy_id(taxonomy_id)
          errors.add(:taxonomy_id, "already exists for this exclusive taxonomy")
        end
      end
    end
    

    Note that this is useless in this case as the first method give the same result but may be useful if you have more complex validation to do.

    For the second part of your question, here is the way to make your query in ActiveRecord, let’s assume taxonomy is the taxonomy for which you want the photos :

    Photo.includes(:terms => :taxonomy).where('taxonomies.id' => taxonomy.id)
    

    UPDATE after your comment

    I believe your validation should take place in the table which make the link between photos and terms. If you have following Rails convention, it’s actually the table photos_terms.
    To add a validation on it, you will have to make it a first class citizen, ie: add an active record model for this table and an id field, let’s say you name it PhotoTermLink.
    Here is the code for the validation you want :

    class PhotoTermLink
      belongs_to :photo
      belongs_to :term
    
      validates :term_id, uniqueness: { scope: :photo_id }, unless: Proc.new { |link| link.term.taxonomy.inclusive }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.