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!
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/wf7Ke60L8zAJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.