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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:18:27+00:00 2026-05-23T17:18:27+00:00

What would you recommend as the best way to refactor these two bits of

  • 0

What would you recommend as the best way to refactor these two bits of view code?

<%if Supplydetail.find_all_by_isbn_id(@isbn).first.nil? %>
<%else%>
<%if Productavailability.find_by_code(Supplydetail.find_all_by_isbn_id(@isbn).first.productsupply_supplydetail_productavailability).nil? %>
<%else%>
<li><%= Productavailability.find_by_code(Supplydetail.find_all_by_isbn_id(@isbn).first.productsupply_supplydetail_productavailability).value %></li>
<%end%>
<%end%>

and (using formtastic)

%li.tip 
  = tooltip(:test, :hover)
= f.input :relatedmaterial_relatedproduct_idvalue, :label => "Related ISBN", :as => :select, :collection => Isbn.all, :label_method => :descriptivedetail_titledetail_titleelement_titlewithoutprefix, :value_method => :productidentifier_idvalue  
%li.list
  = link_to "Edit list", isbns_path 

I have examples of each of these about a bazillion times in my app, and would like to know I’m refactoring in the best way before I dive in to this rather huge job.

  • 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-23T17:18:28+00:00Added an answer on May 23, 2026 at 5:18 pm

    First of all, an empty if branch in an if/else usually (but not always!) smells bad so don’t do that, it just makes your code harder to read and understand.

    Also, you’re computing Supplydetail.find_all_by_isbn_id(@isbn) and Productavailability.find_by_code(...) twice just to get your <li> output. Don’t do that either.

    And you might want to push most of that logic into your controller (or maybe a helper depending on where and how often it is used) to cut down on the ERB-noise.

    Maybe something like this would serve you (and whoever gets to maintain your code) better; a bit of controller stuff first:

    @avail  = nil
    by_isbn = Supplydetail.find_all_by_isbn_id(@isbn).first
    if by_isbn
        @avail = Productavailability.find_by_code(by_isbn.productsupply_supplydetail_productavailability)
    end
    

    And then in your ERB:

    <% if @avail %>
        <li><%= @avail.value %></li> 
    <% end %>
    

    If you’re doing a lot of this sort of thing then you could add a Productavailability.for_isbn convenience class method in your model. Then your controller would just need:

    @avail = Productavailability.for_isbn(@isbn)
    

    But I wouldn’t worry about it until you start repeating yourself.

    I’m not familiar with formtastic so I can’t help you with that.

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

Sidebar

Related Questions

I'm wondering what y'alls would recommend as the best way to go about getting
Would you recommend the best and easy way to transfer (or copy) the subscriptions
Would you recommend me the best approach to edit SQL query with Visual Studio
What are the basic minimum set of Code Analysis rules that you would recommend
Can anyone please recommend the best way to trigger a horizontal Jquery scroller using
Can anyone recommend the best way to detect mobile device accessing a site -
What would be the best way to ensure that a SQL query won't alter
So standard Agile philosophy would recommend making your domain classes simple POCOs which are
Which orm you would recommend for a beginner moving from ado.net?... Thus far used
Are there any tools you would recommend for site architecture design and analysis? I've

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.