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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:11:33+00:00 2026-05-12T15:11:33+00:00

I have numerous statements like these in both my create and update action of

  • 0

I have numerous statements like these in both my create and update action of my controller:

@company.set_preference(:api_username, params[:company]['api_username']) if params[:company]['api_username']
@company.set_preference(:api_password, params[:company]['api_password']) if params[:company]['api_password']

I’m wondering if how I can refactor these to make my code more dry. I’m acutely aware they may belong in the model (re: fat models and skinny controllers), but am not sure what to do. Can someone enlighten me?

Thanks in advance for your time,

Gav

  • 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-12T15:11:33+00:00Added an answer on May 12, 2026 at 3:11 pm

    Is it the repetition of @company.set_preference that you’re trying to avoid? or the repetition of params[:company]..?

    How about adding a method to your Company model like:

    def update_preferences(prefs)
      prefs.each_pair do |pref_name, value|
        set_preference(pref_name.to_sym, value)
      end
    end
    

    and then calling it with

    @company.update_preferences(params[:company])
    

    You could also add a check to ensure only valid preferences are set e.g.

    VALID_PREFERENCES = ['api_username', 'api_password']
    
    def update_preferences(prefs)
      prefs.each_pair do |pref_name, value|
        set_preference(pref_name.to_sym, value) if VALID_PREFERENCES.include?(pref_name)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have numerous link in my View, which are shown like below retailerName1----------info----------anchor-Link-retailer1 retailerName2----------info----------anchor-Link-retailer2
I have numerous index.php scripts using SEO-friendly arg format like example.com/path/to/index.php/opt/arg and would like
I have numerous buttons on my page with the same class names. However these
Here's my situation I have numerous SQL rows like the following. SET IDENTITY_INSERT blah
Basically, I have an excel sheet with numerous select statements; wondering what's the quickest
In the method below there are numerous case statements (many have been removed) that
I have numerous functions (unknown at design time) that each take a specific number
I have numerous Corba servers and some other java apps (not web). I wish
i have strange problem doing reporting: i have numerous clients with different issued invoices.
In my program I have numerous Entity classes with a Texture2D attribute that are

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.