Error in displaying data in Rails Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Error in displaying data in Rails without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to display data in the view for a member’s profile. When I run the inspect option on the variable, it prints out all of the data on the profile variable. But, when I have it only call a column, I get an error.

I run the same code on a different variable and it prints out; so, I am a bit confused on what is going on. Is it because the Active Record has a relation? Here is the code:


def show
  @show_page = params[:id]
  @member = current_member
  @profile = Profile.where(member_id:    


<%= @show_page.inspect %>
<%= @profile.inspect %>
<%= @member.inspect %>

  <strong>Member ID:</strong>
  <%= %>

View in Browser


#<ActiveRecord::Relation [#<Profile id: 6, f_name: "Test", l_name: "Member", u_name: "testing", security: "10", private: "1", avatar: nil, birthday: nil, phone: nil, address1: nil, address2: nil, city: nil, state: nil, zip: nil, long: nil, lat: nil, facebook: nil, twitter: nil, instagram: nil, pinterest: nil, googleplus: nil, motto: nil, created_at: "2017-12-23 05:15:53", updated_at: "2017-12-23 05:15:53", member_id: 8>]>

#<Member id: 8, email: "", created_at: "2017-12-19 20:02:34", updated_at: "2017-12-23 05:15:37">

Member ID: 8

Now, when I add the following code into the show page, I get an error.


  <strong>User Name:</strong>
  <%= @profile.u_name %>


Showing /Users/topher/Dropbox/railsapps/~sandboxes/temporary/app/views/profiles/show.html.erb where line #21 raised:

undefined method `u_name' for #<Profile::ActiveRecord_Relation:0x00007fcb2583b920>
Did you mean?  name

I am just confused on if there is a different way that I need to be calling the data found in the variable. The only difference between the @member and @profile print outs that I can see is the #<ActiveRecord::Relation [ prefixed to @profile. Does this mean that I need to call the information differently?


#where is query method, and returns records matching query conditions wrapped in ActiveRecord::Relation object. Which explains why you are getting this error. To resolve it, you need to change it to:

@profile = Profile.where(member_id:

Which will return first record matching given member id to @profile instead of ActiveRecord::Relation object.

However, you must use a finder method in case you want to find a particular record. So better and cleaner approach would be:

@profile = Profile.find_by(member_id:
We are here to answer your question about Error in displaying data in Rails - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji