In my application I have models User
, Post
, Notification
, PostShare
& SocialPage
.
This is how I've my models associations:
class User < ActiveRecord::Base
has_many :social_pages
has_many :posts
class Post < ActiveRecord::Base
has_many :post_shares
has_many :notifications
belongs_to :user
class Notification < ActiveRecord::Base
belongs_to :post
class PostShare < ActiveRecord::Base
belongs_to :post
belongs_to :social_page
class SocialPage < ActiveRecord::Base
belongs_to :user
has_many :post_shares
When notifications
are added, I'm adding post_id
to my table.
What I want to achieve is, based on the post_id
from notifications
, I want to find post_shares
for that post_id
& find the social_pages_id
& based social_page_id
find which user has own/created the page and show them notifications.
I've tried with joins
like Notification.joins(:post).group(:post_id).select(:post_id)
, but I don't get anything correct.
Any help is appreciated!