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

  • Home
  • SEARCH
  • 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 9176523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:07:36+00:00 2026-06-17T17:07:36+00:00

I have two models that are associated via a has_many relationship. E.g. class Newspaper

  • 0

I have two models that are associated via a has_many relationship. E.g.

class Newspaper < ActiveRecord::Base
  has_many :articles
end

class Article < ActiveRecord::Base
  belongs_to :newspaper

  validates :uid, presence: true,
                  uniqueness: { case_sensitive: true }
end

A newspaper is updated several times a day but we only want to construct and add articles to the association that do not already exist. The following code was my first cut of achieving this.

new_articles.each do |article|
  unless newspaper.articles.exists? uid: article.uid
    newspaper.articles.build(uid: article.uid)
  end
end

The newspaper object is either new and unsaved, or retrieved with existing relationships at this point.

My tests indicate that I am able to add two articles to the newspaper that have the same UID using the code above and this is obviously not want I want.

I appears to me that my current code will result in a validation failure upon being saved as the validation looks at uniqueness across the entire articles table and not the association.

What I’m struggling to understand is how the exists? method behaves in this scenario (and why it’s not saving my bacon as I planned). I’m using FactoryGirl to build a newspaper, add an article and then simulate an update containing an article with the same uid as the article I’ve already added. If the code works I should get only one associated article but instead I get two. Using either build or create makes no difference, thus whether the article record is already present in the database does not appear to change the outcome.

Can anyone shed some light on how I can achieve the desired result or why the exists? method is not doing what I expect?

Thanks

  • 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-17T17:07:37+00:00Added an answer on June 17, 2026 at 5:07 pm

    The association exists? actually creates a scoped query, as per the association. This is why your existing articles filter doesn’t work.

    unless newspaper.articles.exists? uid: article.uid
    
    # `articles.exists?` here will produce this if the newspaper is new 
    #   ... WHERE "articles"."newspaper_id" IS NULL AND "articles.uid" = '<your uid>'
    
    # and this, if the newspaper is persisted (with an id of 1)
    #   ... WHERE "articles"."newspaper_id" = 1 AND "articles.uid" = '<your uid>'
    

    The case of the new newspaper is clearly wrong, as it would only return articles with a nil newspaper ID. But the persisted case is probably undesirable as well, as it still unnecessarily filters against newspaper ID, when you real concern here is that the UID is unique.

    Rather, you probably want simply against Article, rather than scoping the exists? through the association, like:

    unless Article.exists? uid: article.uid
    

    Concerning your other problem:

    this appears to be a FactoryGirl problem where the create method isn’t creating db entries in the same way I can in the irb.

    FactoryGirl.create should still abide by validations. It might help to see your test.

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

Sidebar

Related Questions

I have two models, Page and PageContent. class Page < ActiveRecord::Base has_many :page_contents end
I have two Models here associated as such: class Order < ActiveRecord::Base belongs_to :customer
I have two models in a has_many relationship such that Log has_many Items. Rails
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models, associated with a HABTM (actually using has_many :through on both
I have two models that are associated. I have an Artist model and a
So, I have two models, User and Thing. User has_many :things end Thing belongs_to
I have two models where A has_many B. If I load A including associated

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.