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?
find_by_attribute will always return only one result. You need to change your code to:
or even better: