【发布时间】:2018-01-28 14:06:20
【问题描述】:
在我的应用程序中,我有模型 User、Post、Notification、PostShare 和 SocialPage。
这就是我的模型关联方式:
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
当添加notifications 时,我会将post_id 添加到我的表中。
我想要实现的是,基于来自notifications 的post_id,我想为那个post_id 找到post_shares 并找到social_pages_id 和基于social_page_id 的用户有拥有/创建页面并向他们显示通知。
我已经尝试过使用joins,比如Notification.joins(:post).group(:post_id).select(:post_id),但我没有得到任何正确的结果。
感谢任何帮助!
【问题讨论】:
标签: ruby ruby-on-rails-4 join model controller