Q:

Passing additional information through controllers

I have a comment model and when a comment is created it makes a note of the profile_name of the user and saves it. So it is basically saving @user.profile_name as comment.profile_name. 
If I now want to show additional information from the user who has that profile_name such as @user.avatar - how would I query it without having to add extra fields to be saved when a comment is created?
I Imagined I could do something like 
@user = User.all
@comment_user = User.where(:profile_name => @user.profile_name)

And then run 
<% @comment_user.each do |user| %>
  <%= user.first_name %>
<% end %>

In the view but I get an error 
undefined method `profile_name' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x007fc2081c7290>

I'm not even sure if that is the correct way to proceed even if I didn't get the error.

A:

The error is because you are trying to call profile_name on User.all, but what you want is a single user.  Try:
@user = User.first
@comment_user = User.where(:profile_name => @user.profile_name)

That being said, you should be using ActiveRecord associations instead. Instead of storing the user's profile_name in the comment record, you should store the user_id .  Create a belongs_to :user association in the Comment model and then you can access the other user attributes directly.
First add a :user_id integer column to the the comments table, and then define your associations:
class Comment < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :comments
end

When creating a new comment, you can do something like:
@comment = @user.comments.create(text: 'my awesome comment')

Then in your view:
<%= @comment.text %>
<%= @comment.user.profile_name %>

