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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:33:39+00:00 2026-05-16T17:33:39+00:00

I have a private method in my Rails app to connect to Amazon S3,

  • 0

I have a private method in my Rails app to connect to Amazon S3, execute a passed block of code, then close the connection to S3. It looks like so;

def S3
  AWS::S3::Base.establish_connection!(
    :access_key_id     => 'Not telling',
    :secret_access_key => 'Really not telling'
  )
  data = yield
  AWS::S3::Base.disconnect
  data
end

It is called like this (as an example);

send_data(S3 {AWS::S3::S3Object.value("#{@upload_file.name}",'bucket')}, :filename => @upload_file.name)

I call this method in a number of ways in my controller and model so have it included in both classes as a private method. This works fine and I’m happy with it but it’s not very DRY.

How can I make this method accessible to both my model and controller but only have the code appear once? This is more of a Ruby question than a Rails question and reflects my newness to OOP. I’m guessing a module or a mix-in is the answer but I haven’t really been using either of these up until now and need a little hand-holding.

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-05-16T17:33:40+00:00Added an answer on May 16, 2026 at 5:33 pm

    Modules are used for 3 different things in ruby. First is namespacing. Having class or constant definitions inside a module won’t collide with classes or constants outside that module. Something like this

    class Product
      def foo
        puts 'first'
      end
    end
    
    module Affiliate
      class Product
        puts 'second'
      end
    end
    
    p = Product.new
    p.foo # => 'first'
    
    p = Affiliate::Product.new
    p.foo # => 'second'
    

    The second use for modules is as a place to stick methods that don’t really have a place anywhere else. You can do this inside a class too, but using a module sort of tells people reading the code that it is not meant to be instanciated. Something like this

    module Foo
      def self.bar
        puts 'hi'
      end
    end
    
    Foo.bar #=> 'hi'
    

    Finally (and the most confusing) is that modules can be included into other classes. Using them this way is also referred to as a mixin, because you are “mixing in” all the methods into whatever you are including.

    module Foo
      def bar
        puts 'hi'
      end
    end
    
    class Baz
      include Foo
    end
    
    b = Baz.new
    b.bar #=> 'hi'
    

    Mixins are actually a way more complected topic then I am covering here, but going deeper would probably be confusing.

    Now, to me, S3 seems to be something that really belongs in the controller, since controllers are usually the things dealing with incoming and outgoing connections. If that is the case, I would just have a protected method on application controller, since that will be accessible to all other controllers, but still be private.

    If you do have a good reason for it being in the model too, I would go for a mixin. Something like

    module AwsUtils
    private
      def S3
        AWS::S3::Base.establish_connection!\
          :access_key_id     => 'Not telling',
          :secret_access_key => 'Really not telling'
    
        data = yield
        AWS::S3::Base.disconnect
        data
      end
    end
    

    If you put that in lib/aws_utils.rb, you should be able to use it by adding include AwsUtils in both your controller and your model. Rails knows to look for classes and modules in lib, but only if the name matches (in wide case). I called it AwsUtils because I know what rails will look for when it sees that (aws_utils.rb), and to be honest, I have no idea what it will need for S3Utils 😉

    Feel free to ask for more info if I wasn’t clear on something. Modules tend to be one of those things in ruby that while amazing, are downright baffling to newcomers.

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

Sidebar

Related Questions

I have a private method def __pickSide(self): in a parent class that I would
In my messages_controller I have the following private method: def find_message_or_404(slug) message = user.messages.find_by_slug(slug)
I have a method private static DataTable ParseTable(HtmlNode table) and sometimes this method has
I have a class A which has a private method called a(). I have
I have a problem getting a private method using reflection. Even with BindingFlags.NonPublic and
Is there a convention for naming the private method that I have called _Add
I'm trying to unit test a private method that I have attached to my
I have class which have one public method Start , one private method and
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
I have the C# method private static string TypeNameLower(object o) { return o.GetType().Name.ToLower(); }

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.