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.