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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:00:04+00:00 2026-05-27T02:00:04+00:00

I’m working on an internal tool. Here are the three main models im working

  • 0

I’m working on an internal tool. Here are the three main models im working with:

Site
  has_many Document
    has_many Version

The document model essentially stores a body of content. What I want to do is setup document versioning with the Version model. So the version would store the content.

Right now, I create a new document via the nested URL: POST /sites/:id/documents. Since the form is there, and I have no plans on changing the user experience here, I started building it to use both the Document and Version models. However, I’m starting to feel like this is wrong. Managing validations from a child object and pushing them up to the parent is becoming a pain.

Another note:

The Document has an integer field called active_version_id. It stores the version that is currently the active one of that document. I then use that ID to get the version model, to show the main content connected to that document. I set it to the version created when a document is created.

What i want to do

In short, here’s the workflow I would expect:

  1. Goes to create new document
  2. Puts in the content
  3. Submits the form
  4. Parent Document is created.
  5. Using the parent document, it creates a new version. Any validation errors on the content at the version level should prevent the save of both models.
  6. Errors from the version object should show up on the page, so the user knows.

Here’s my Version model, so you can see the validation logic. It basically checks if the headers in the document are valid (which is just some identification text). And that the headers aren’t used in versions of any other document in that site:

def document_is_in_valid_format
  p = CopyProcess::Processor.new
  if !p.contains_valid_headers(self.content)
    Rails.logger.debug "Headers invalid."
    msg = "Content headers must be in valid format."
    self.document.errors[:base] << msg
    self.errors[:base] << msg
  else
    # Check that headers are unique to the parent document
    headers = content.split(/\n/)[1..3].join(' - ').gsub(/\/\*|\\\*/,'')
    site_documents = []
    # get all documents where it's not this one's parent
    if self.id
      site_documents = Document.where(["site_id = ? AND id <> ?", self.site_id, self.document_id])
    else
      site_documents = Document.where(["site_id = ?", self.site_id])
    end
    site_documents.includes(:versions)
    site_documents.each do |doc|
      if doc.version_names.include?(headers)
        msg = "Content headers are not unique."
        self.document.errors[:base] << msg
        self.errors[:base] << msg
        break
      end
    end
  end
end

I’m wondering if the solution here might be to use a nested form for. For step 5, i imagine a transaction would help prevent both saves.

  • 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-27T02:00:05+00:00Added an answer on May 27, 2026 at 2:00 am

    Check in http://api.rubyonrails.org for:

    • validates_associated
    • accepts_nested_attributes_for

    Also take into account that you can use validations on _id/_ids fields, like validates :document_ids, presence: true to make sure there is at least one document.

    To make the code more readable i would try split your long validation sequence in custom validators using:

    • ActiveModel::Validator
    • ActiveModel::EachValidator
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.