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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:04:50+00:00 2026-05-17T15:04:50+00:00

New to Ruby and Rails. I am developing a simple app for where you

  • 0

New to Ruby and Rails.

I am developing a simple app for where you can register teams, players etc and it looks like this:

Team has_many Players
Players belongs_to Team

When I want to show the player in view(normal users):

<%= @player.name %> - <%= playerteam %>

and in the admin view it looks like this:

<% @players.each do |player| %>
    <tr>        
        <td><%= player.id %></td>   
        <td><%= player.name %></td>
        <td><%= playerteam(player) %></td>
        <td><%= owner(player) %></td>               
    </tr>
<% end %>

and the helper method:

def playerteam(player = nil)      
  if player != nil
    if player.team_id == nil
      return "No team"
    else
      @team = Team.find(player.team_id)
      return @team.name
    end
  else              
    if @player.team_id == nil
      return "No team"      
    else
      @team = Team.find(@player.team_id)
      return @team.name
     end
  end
end

It works but it is not pretty or “Ruby Sexy”

At first it was only used from the normal view but then when I wanted to use it from the admin-view also I had to add the parameter with a default value and the extra if-clause.

Are there a better way?

  • 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-17T15:04:51+00:00Added an answer on May 17, 2026 at 3:04 pm

    I wouldn’t use a helper method at all. Helper Methods shouldn’t be used to retrieve model data. That’s what’s the model is for.

    you can do something like this:

    normal users:

    <%= @player.name %> - <%= player.team ? player.team.name : 'No Team' %>
    

    admin:

    <% @players.each do |player| %>
        <tr>        
            <td><%= player.id %></td>   
            <td><%= player.name %></td>
            <td><%= player.team ? player.team.name : 'No Team' %></td>
            <td><%= owner(player) %></td>               
        </tr>
    <% end %>
    

    You could probably do something similar with the owner-helper

    to avoid the ? :-if-else syntax in every view you can add this to your player model

    class Player < ActiveRecord::Base
      def team_name
        team ? team.name : "No Team"
      end
    end
    

    Then you views look like this:

    normal users:

    <%= @player.name %> - <%= @player.team_name %>
    

    admin:

    <% @players.each do |player| %>
        <tr>        
            <td><%= player.id %></td>   
            <td><%= player.name %></td>
            <td><%= player.team_name %></td>
            <td><%= owner(player) %></td>               
        </tr>
    <% end %>
    

    IMHO: This would be much more “Rails Sexy” 😉

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

Sidebar

Related Questions

I'm new to Ruby on Rails...I've been playing around with developing a social networking
I am new to Ruby on Rails, I have completed the Blog Tutorial .
New to both Ruby and Rails but I'm book educated by now (which apparently
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
I'm new to ruby and started to create my *nd toy app. I: Created
I'm fairly new in the Ruby + Rails scene. Although I have a very
I am new to both Django and Rails. I am thinking of developing an
I've only just started learning ruby on rails and I would like to create
I am developing a Ruby on Rails application with multilingual support. Now I have
I'm developing a Ruby on Rails application that needs to allow the user to

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.