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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:05:47+00:00 2026-05-17T01:05:47+00:00

I have a controller with two different actions, but both need this same code,

  • 0

I have a controller with two different actions, but both need this same code, which is a little long, how can I allow them access to this same behavior but keep it DRY?

   @list = Contact.find :all,
      :select => "companies.name AS co_name, 
                  companies.id AS comp_id, 
                  COUNT(contact_emails.id) AS email_count, 
                  COUNT(contact_calls.id) AS call_count, 
                  COUNT(contact_letters.id) AS letter_count, 
                  COUNT(contact_postalcards.id) AS postalcard_count",

      :conditions => ['contact_emails.date_sent < ? and contact_emails.date_sent > ?', 
                      report_end_date, report_start_date],

      :joins => [
        "LEFT JOIN companies ON companies.id = contacts.company_id",
        "LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
        "LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
        "LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
        "LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
      ],
      #:group => "companies.id"
       :group => "companies.name"
    puts @list[0].attributes.inspect
  • 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-17T01:05:47+00:00Added an answer on May 17, 2026 at 1:05 am

    You should move this code to model:

    # Contatct model
    
    def self.get_list(report_start_date, report_end_date)
      self.find :all,
        :select => "companies.name AS co_name, 
                  companies.id AS comp_id, 
                  COUNT(contact_emails.id) AS email_count, 
                  COUNT(contact_calls.id) AS call_count, 
                  COUNT(contact_letters.id) AS letter_count, 
                  COUNT(contact_postalcards.id) AS postalcard_count",
    
        :conditions => ['contact_emails.date_sent < ? and contact_emails.date_sent > ?', 
                      report_end_date, report_start_date],
    
        :joins => [
          "LEFT JOIN companies ON companies.id = contacts.company_id",
          "LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
          "LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
          "LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
          "LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
        ],
        #:group => "companies.id"
        :group => "companies.name"
    end
    

    Then you can use it in controllers:

    @list = Contact.get_list(report_start_date, report_end_date)
    

    Probably you can also split it to smaller parts and use scopes and defined associations instead of writing all of it on your own.

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

Sidebar

Related Questions

I have two actions in my controller which are sharing part of logic responsible
I have form with two buttons having same name but different values ,how to
I would need to have two different projects, let's say internal and external, which
I have an action inside my controller class and I want two different routes
I have two view controller classes in my application delegate. I can change from
I have two actions in my controller : def find @item = Item.find(params[:id]) render
lets say I have two methods in my controller to support both json and
I can't seem to find a method to call different controller in the same
Is it possible to have two inputs, or button controls, trigger two different actions
I have a controller that extends Zend_Controller_Action. It contains some actions that I need

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.