Merging feed from two different tables.

Hello, good day!

I am developing a Quotation app. It has Users, Authors and Quotes.

Users are normal people with random quotes and Authors are famous people with famous quotations.

  • User has_many Quotes

  • Quote belongs_to User

  • Author has_many Quotes

  • Quote belongs_to Author

The two tables would be exactly the same but the Author’s table have minimal security functions because any given User could create a new Author profile or edit an Author profile.

A User can follow another User or subscribe to an Author.

  • User has_many Relationships, :foreign_key => “follower_id”

  • User has_many reverse-Relationships, :foreign_key => “followed_id”

  • Relationship belongs_to :follower, :class_name => “User”

  • Relationship belongs_to :followed, :class_name => “User”

  • User has_many Subscriptions, :foreign_key => “user_id”

  • Subscription belongs_to :user

  • Subscription belongs_to :author

With that said, I have an issue I can’t get around.

I managed to get a “feed” working for a User who is following another user to see this user’s Quotes. With the following codes:

USER.rb

def feed

Quote.from_users_followed_by(self)

end

MICROPOST.rb

scope :from_users_followed_by, lambda { |user| followed_by(user) }

def self.followed_by(user)

following_ids = %(SELECT followed_id FROM relationships

WHERE follower_id = :user_id)

where(“user_id IN (#{following_ids}) OR user_id = :user_id”,

{ user_id: user })

end

And I also managed to duplicate these functions, and get a feed for the authors:

USER.rb

def feed

Quote.from_authors_idols_of(self)

end

MICROPOST.rb

def self.idols_of(user)

idols_ids = %(SELECT author_id FROM subscriptions

WHERE user_id = :user_id)

where(“author_id IN (#{idols_ids}) OR user_id = :user_id”,

{ user_id: user })

end

Although these functions are working, I can’t get them to work together, only separately.

What would be the best approach to collect the quotes from the Users, and the quotes from the Authors, and show them all in the same feed, following a created_at order?

Thanks a lot for your time!

Two things here are the clues to the basic problem. First the two
tables are virtually the same, and second you are trying to merge the
data from the two tables. The solution is to have only one table
containing both, and either have a flag to identify the type or
possibly use roles to distinguish. Have a look at the cancan gem for
this. Though if you are certain there will only ever be the two types
then I would stick with a flag.

A tip here just in case you are not already doing it. Use a Version
Control System (I prefer git) for controlling your code. Then you can
easily setup a branch to do the changes on even if you are not
convinced it is the right way to go. Then after experimenting to see
how it goes you can either merge the branch back into trunk and carry
on with the new strategy or easily wind back the migrations and revert
to the base of the branch to get back to where you started.

Colin