Fetching all posts made by a user with Devise gem

I'm using Devise to manage user model.

Here's the User model, user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  has_many :topics

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable        


Here's the Topics model, topic.rb

class Topic < ActiveRecord::Base
    belongs_to :user

When I create a post, the user ID is stored in the "user_id" column of Topics. I want to a User profile page, which should display all the posts he has made. In my User profile page view, I have,

<%= Topic.find_by_user_id(@user.id) %>

I have two posts made by the same user,

But when I try fetch all the posts made by the user, the SQL command automatically queries with a LIMIT of 1. Like this,

And because of this, the view is fetching only 1 post made by the user. How do I fetch all the posts?


find_by_attribute will always return only one result. You need to change your code to:

Topic.where(user_id: @user.id)

or even better:


Need Your Help

About send a picture on posting an event

php facebook events

I would like to upload a picture to Facebook event.

Visual Studio 2008 - jump to line number shortcut

visual-studio visual-studio-2008 ide keyboard-shortcuts

I can go to a specific line number by double clicking in the status bar in Visual Studio. Is there a keyboard shortcut that does the same thing?