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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:28:37+00:00 2026-06-11T00:28:37+00:00

How could I DRY out the following piece of Rails code? The type parameter

  • 0

How could I DRY out the following piece of Rails code? The type parameter will correspond to Rails scopes, and I had assumed that all would work too … but it returns an array and my cunning plan fails.

The if looks wrong to me, but I can’t figure out how to have a all scope work correctly; any ideas?

Original code that improperly relied on all as a scope:

def readable(type=:all)
  StudyAid.send(type.to_s).authored_by(self)
end

Working but ugly version:

def readable(type=:all)
  if type == :all
    StudyAid.authored_by(self)
  else
    StudyAid.send(type.to_s).authored_by(self)
  end
end
  • 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-11T00:28:39+00:00Added an answer on June 11, 2026 at 12:28 am

    I wouldn’t try and modify the behaviour of the all method because that could unexpectedly change the semantics of other parts of your code and would surprise other people who came to work with your code.

    However you could define a named scope on StudyAid with a different name e.g.

    named_scope :every
    

    and then your method could become:

    def readable(type=:every)
      StudyAid.send(type).authored_by(self)
    end
    

    Minor point: you can use symbols with send so your to_s call isn’t needed.

    Finally, I don’t think your version is as ugly as you think yourself.

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

Sidebar

Related Questions

I want to DRY my source code. Let's say I had some of these
I am trying to DRY up a bunch of forms code that has a
Could someone know where is saved path to external XML-File that contain configuration for
Could someone please suggest why this is happening... I’ve got some code to pretty
I am really trying to follow the DRY principle. I have a sub that
After finding out the hard way that clone() does not work as intended for
I like to keep my code as DRY as possible and would like to
I am having some problems creating a loop that could show the cheapest price.
I am trying to DRY up my code a bit so I am writing
In CVS I could do the following cvs -n update And it would should

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.