1

I'm building a rails app where a user model has several has_many relationships with other models.

class User < ActiveRecord::Base

    has_many :model_2s
    has_many :model_3s
    has_many :model_4s

end

I am then creating a profile page where I display data from each of these models for the current user. I am doing this using a separate controller - profiles_controller.rb

In the profiles controller I am creating instance variables for each of these models based on the current user:

class ProfilesController < ApplicationController

    @user = User.find(params[:id])
    @model_2s = @user.model_2s.all
    @model_3s = @user.model_3s.all
    @model_4s = @user.model_4s.all
end

I am then able to display data from these instance variables in the show view for profiles. I do this by calling an each method.

The functionality I am now trying to add is to show some default data (like Lorem Ipsum placeholder text) for fields that the user has not filled in.

My issue is that I cannot figure out how to check if a particular field in these instance variables is empty.

For example, I would like something along the lines of:

<% if @model_2s.field_1.empty? %>
    <p>Lorem Ipsum</p>
<% else %>
    <%= @model_2s.field_1 %>
<% end %>

However this gives an "undefined method 'field_1'... " error.

I know how to check for @model_2s.any? to confirm the instance variable itself is present, but I want to be able to do a conditional check at the next level down - the fields within the instance variable.

7
  • 1
    @model_2s is an enumerable (array). You can test it's empty with @model_2s.empty? or you can test a particular element with @model_2s.first.field_1.blank? (here the first element is checked) Commented May 27, 2013 at 8:09
  • thanks... unfortunately after adding '.first' I still get the same error... "undefined method 'field_1'... " Commented May 27, 2013 at 8:15
  • Then your @model_2s array is empty. You will first have to check whether anything is found at all, probably by doing something like if @model_2s.empty? || @model_2s.first.field_1.blank?. Commented May 27, 2013 at 8:39
  • use this "@model_2s.respond_to?('field_1')" Commented May 27, 2013 at 9:29
  • @JaapHaagmans - model_2s is not empty, I am able to loop over it and pull out other fields. I think the issue as per the answers below is that I was trying to do the if outside of an each loop. Commented May 27, 2013 at 10:30

2 Answers 2

1

Not sure what you're doing in your view, but definitely you need to loop each of those result sets if you want to access something like field_1

<% @model_2s.each do |m| %>

    <% if m.field_1.empty? %>
        <p>Lorem Ipsum</p>
    <% else %>
        <%= m.field_1 %>
    <% end %>

<% end %>

This also handles the case when @model_2s is empty.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, thanks. Couple of questions - 1. Is there an easy explanation as to why you can only access the fields of an instance variable by using an each loop? and 2. If I need to do this over a number of models in my view, should I be setting them up as helper methods?
1) because you're working with an array (actually an ActiveRecord::Relation which behaves as an Array), and the way you deal with arrays is usually by looping :). 2) yes, of course, you can use helper methods to clean up the view code
0

Try this way

<% if @model_2s %>
 <% @model_2s.each do |model_2| %>
  <% if model_2.field_1.empty? %>
      <p>Lorem Ipsum</p>
  <% else %>
      <%= model_2.field_1 %>
  <% end %>
 <% end %>
<% end %>

It will be inside the loop..

2 Comments

you can not call field_1 on an enumerable. may be you can loop through @model_2s to get the object, the method/attribute (field_1) is defined for.
thanks @Thahakp - seems the same point as the other answer - I needed to be looping through the instance variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.