I updated the question because I combined following locations and users into one polymorphic model.
So I am trying to make a news feed for comments in my app.
I have a user model, a model for following locations and users. And then I have a model for comments.
How do I grab the comments from users that a user follows and comments from a location that a user follows and put these together?
I need this to be paginated as well as sorted by the created_at time stamp ( showing newest first).
Heres what my migrations look like
create_table :comments do |t|
t.text :text
t.integer :user_id
t.integer :commentable_id
t.string :commentable_type
create_table :follows do |t|
t.integer :user_id
t.integer :followable_id
t.string :followable_type
And this is what my models look like
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
update: I found something called a CASE statement in sql (like an if statement) How do I perform an IF...THEN in an SQL SELECT?
I am still unsure of how to write this query but I think the CASE statement may help.