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

  acts_as_voter
  has_many :topics

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

end

Here's the Topics model, topic.rb

class Topic < ActiveRecord::Base
    acts_as_voteable
    belongs_to :user
end

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?

Answers


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:

@user.topics

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?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.