Ruby on rails 使用连接模型rubyonrails进行过滤

Ruby on rails 使用连接模型rubyonrails进行过滤,ruby-on-rails,activerecord,rails-activerecord,Ruby On Rails,Activerecord,Rails Activerecord,我需要显示来自具有活动订阅的用户的帖子 型号/用户.rb has_one :profile, dependent: :destroy has_many :posts, dependent: :destroy belongs_to :user, dependent: :destroy model/profile.rb->suscription belongs_to :user, dependent: :destroy scope :active, -> (as_of_date = Dat

我需要显示来自具有活动订阅的用户的帖子

型号/用户.rb

has_one :profile, dependent: :destroy
has_many :posts, dependent: :destroy
belongs_to :user, dependent: :destroy
model/profile.rb->suscription

belongs_to :user, dependent: :destroy
scope :active, -> (as_of_date = Date.current) { where('? BETWEEN start_suscription AND end_suscription', as_of_date) } // return true is active
model/post.rb

has_one :profile, dependent: :destroy
has_many :posts, dependent: :destroy
belongs_to :user, dependent: :destroy

如何进行查询?

请尝试下面的模型查询

Post.joins(user: :profile).where('? BETWEEN start_suscription AND end_suscription', Date.current)

这将生成与
@mohanraj
的答案相同的查询。但是,使用此解决方案时,您不必重复现有代码(DRY原则)。

您能在控制台中检查此查询吗?Post.joins(user::profile).where(‘?介于开始脚本和结束脚本之间’,Date.current)。我没有办法检查这个问题。@Mohanraj它是有效的,谢谢,让我补充一下答案。