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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:06:05+00:00 2026-06-13T21:06:05+00:00

I’m not sure the title really explains correctly what i’m trying to do so

  • 0

I’m not sure the title really explains correctly what i’m trying to do so forgive me. I have a table called clients for an invoicing system i’ve built. I also have tables called projects and invoices.

Here’s an example (not the actual columns in the tables, just for an example) of my database structure.

Clients
    id
    name
    description
    archive

Projects
    id
    client_id
    name
    description
    archive

Projects
    id
    client_id
    project_id
    name
    description
    archive

I have created the ability to archive a given invoice, project, or client independently. However, i wanted to see what the best way to do this would be if i wanted to have say the projects and invoices for a client to be automatically marked as archived when i archive the client. My process would be this:

  • Archiving a client would archive the client, all projects with that client id, and all invoices with that same client id.
  • Archiving a project would archive the project and all invoices with that project id.
  • Archiving an invoice just archives the invoice itself

NOTE: by “archived” i mean the archive bit gets flipped to 1 instead of 0 (not archived / active).

By the way, the way that you archive something is on the edit screen there is an “Archive?” checkbox that you would check off to archive the item.

I know that in my edit action in my controller i can just say something like if archive == 1 then run additional sql update statements to update dependent items else @object.save to just save the data for the edited item.

My question?

Is there a better way to do this? Is the way i mentioned generally the best practice? I am already using :dependent => :destroy elsewhere in the model to delete rows in associated tables when a “parent” item is deleted. Is there something pre-built in to Rails that will allow me to mark things like i am trying to do through the relationships of the tables?

Thanks for any help with this! Hope i explained the problem and question well enough, but let me know if you have questions.

  • 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-13T21:06:06+00:00Added an answer on June 13, 2026 at 9:06 pm

    A better way would be to create a after_update on the Client and Project model :

    class Client < ActiveRecord::Base
      after_update :archive_check
    
      private
      def archive_check
        projects.each{|p| p.archive = self.archive; p.save} if self.archive_changed?
      end
    end
    
    class Project < ActiveRecord::Base
      after_update :archive_check
    
      private
      def archive_check
        invoices.each{|i| i.archive = self.archive; i.save} if self.archive_changed?
      end
    end
    

    So when you save a client, the archive_check is triggered. And then if archived, it’ll update all the related projects. Since you update the projects, the archive_check will also be triggered in the Project model for each invoice this time.

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.