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

  • Home
  • SEARCH
  • 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 7368359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:39:35+00:00 2026-05-29T03:39:35+00:00

I created a Micropost model that have the following attributes: <Micropost id: 1, content:

  • 0

I created a Micropost model that have the following attributes:

<Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-29 11:07:53", title: "asdasdad">

an User model with the following attributes:

<User id: 1, email: "alex@gmail.com", username: nil, etc...>

and a Comment model with the following attributes:

<Comment id: 1, content: "sdf", micropost_id: 1, user_id: nil, created_at: "2012-01-29 11:10:42", updated_at: "2012-01-29 11:10:42">

So far, I’ve only accomplished this:

  • Display the username of the author of a micropost
  • Display the ID of the authors of the microposts’ comments

    <h2>Micropost Index</h2>
    <% @microposts.each do |micropost| %>
    
    <%= micropost.title %></td>
    <%= micropost.content %></td>
    <%= link_to 'Show', micropost %></td>
    <%= link_to 'Edit', edit_micropost_path(micropost) %></td>
    <%= link_to 'Destroy', micropost, confirm: 'Are you sure?', method: :delete %>     
    
    <h2>Comments</h2>
    
    <% @micropost.comments.each do |comment| %>
      <p>
        <b>Comment:</b>
        <%= comment.content %>
      </p>
      <p>
        <b>Commenter</b>
        <%= comment.user_id %>
      </p>
    <% end %>
    
  • I don’t know how to create a link to the authors profile (e.g. mysite.com/users/1).

  • I don’t how to retrieve the name of the author of a comment and the link to his/her profile

EDIT:

Models:

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username

  has_many :microposts
  has_many :comments

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
      user
    else # Create a user with a stub password. 
      User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end
  end
end

micropost.rb:

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content

  belongs_to :user
  has_many :comments
end

comment.rb:

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :micropost
  belongs_to :user
end

Micropost controller:

controllers/microposts.rb

  def show
    @micropost = Micropost.find(params[:id])
  end

  def new
    @micropost = Micropost.new
  end

  def create
    @user = current_user
    @micropost = @user.microposts.new(params[:micropost])
    @micropost.save
    redirect_to @micropost
  end

Any suggestions to accomplish this?

  • 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-29T03:39:36+00:00Added an answer on May 29, 2026 at 3:39 am

    To make a link to the user you can use

    <%= link_to comment.user.username, comment.user %>
    

    In general part of the “Rails Magic” is, that you if you set up an association correctly, that you can access the related objects through the dot notation. That means, you don’t need to say comment.user_id but instead go directly for the associated user object, e.g. comment.user.username or comment.user.email … you get the idea 🙂

    In order to to this you should have set up your models like this:

    class User < ActiveRecord::Base
      validates_presence_of :username #username should obviously not allow nil values
      has_many :microposts
      has_many :comments
    end
    
    class MicroPost < ActiveRecord::Base
      belongs_to :user
    end
    
    class Comment < ActiveRecord::Base
      belongs_to :user
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are looking to have Sender and Receiver attributes for each micropost that is
I have a model that I created in 3ds Max. The model is a
I have an existing database(myDatabse) that I created with Microsoft Sql Managenent studio and
I have the following simple custom control that I have defined for a Windows
I have an event that I am subscribing to in a View Model. The
Ok... So I have created my model using EF4. Great! I then turned off
I have created a simplified version of my code that experiences the same issue.
I created a method on a micropost model which takes an array of user_ids.
I have a stored procedure that I've mapped in my entity framework model (trying
Using Microsoft's designer for the Entity Framework (v3.5), I have created an Entity Model

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.