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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:15:46+00:00 2026-05-25T18:15:46+00:00

I have a form and when its posted, some of its data gets saved

  • 0

I have a form and when its posted, some of its data gets saved into the database using the rails model. Along with the database, I need to store some of the content into a file. Can I enhance the “save” method in the model to write the content to the file? Is this a good design. If not what would be ideal design.

Continuing on this, I want to set the location where this file is stored, in the application configuration. Which file should I define this variable for the file location and how do I access it in the model/controller

Thanks
Kiran

  • 1 1 Answer
  • 1 View
  • 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-25T18:15:47+00:00Added an answer on May 25, 2026 at 6:15 pm

    You can of course use one of the existing, ready-made file-attachment libraries, such as paperclip and carrierwave.

    Otherwise, you can:

    # config/application.rb
    # ...
    config.my_app.cache_file_prefix = "/tmp/files"
    
    # app/models/my_model.rb
    class MyModel < ActiveRecord::Base
    
      # Causes ActiveRecord to run this method
      # before saving (creating or updating).
      before_save :copy_to_file
    
    private
    
      def copy_to_file
        # Write data to the file.
        file_name = copy_to_file_name
        File.open(file_name) do |f|
          f.write("some data")
        end
      end
    
      def copy_to_file_name
        # Calculate and return the expected file name.
        prefix = Rails.configuration.my_app.cache_file_prefix
        "#{prefix}/#{id}"
      end
    
    end
    

    Please note that this solution will not work once you have more than one server running your Rails application. You should consider using either an object storage provider (such as S3 or Rackspace) or a replicated or distributed file system (such as DRDB or GlusterFS).

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

Sidebar

Related Questions

I have an html form. I need to send its data to flash on
I have a form which POST's its data via AJAX: $.ajax({ type: POST, url:
I have a form with most of its functionality implemented using standard TAction. I
I have a object with some data being posted to a php script from
I would like to have a form that gets output into a table. The
I have a form that you can add data to a database. It is
I have a form named reg.php and its action is reg.php , i want
I have a form field which requires a json object as its value when
I have a form, and want to disable/enable its submit button depending on whether
i have a web form and i want to change its titles for each

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.