Ruby on Rails - Joins models for getting notifications -
in application have models user, post, notification, postshare & socialpage.
this how i've 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 added, i'm adding post_id table.
what want achieve is, based on post_id notifications, want find post_shares post_id & find social_pages_id & based social_page_id find user has own/created page , show them notifications.
i've tried joins notification.joins(:post).group(:post_id).select(:post_id), don't correct.
any appreciated!
rails nested includes or nested joins on activerecord solve problem here
rails - nested includes on active records?
you can this
notifications = notification.includes(post: [:user, post_shares: [social_page: :user]]).where(post_id: your_post_id) or
notifications = notification.joins(post: [post_shares: [social_page: :user]]).where(post_id: your_post_id) and can access post & social page users this
post_user = notifications.map(&:post).map(&:user) post_shares = [] notifications.each |n| post_shares += n.post.post_shares end social_page_users = post_shares.map(&:social_page).map(&:user)
Comments
Post a Comment