查询通过导轨的has_many [英] Query for has_many through rails
问题描述
协会
类活动
的has_many:views_logs
的has_many:用户,通过:views_logs
结束
我想获得的所有用户
在广告系列
的具体日期等内
意思,我想要做的事,如:
Campaign.first.users.where(?)
现在,当我查询:
Campaign.first.users.all
我得到所有用户的列表中的某个广告系列中,但我如何才能得到只有那些拥有这些用户的具体日期间仅运动范围。
我想获得广告活动中所有用户的任何特定日期内
块引用>#应用程序/模型/ campaign.rb
一流的运动< ActiveRecord的::基地
的has_many:用户,通过:view_logs做
之间的日期,结束日期DEF
其中(created_at:start_date..end_date)
结束
结束
结束@campaign = Campaign.find点¯x
@users = @ campaign.users.between(Date.today - 2,Date.today.day)-
更新 - 这似乎PGSQL需要时间戳评估日期:
@users = @ campaign.users.between(Time.now.midnight - 2.days,Time.now.midnight)
Association is
class Campaign has_many :views_logs has_many :users, through: :views_logs end
I want to get all
users
in aCampaign
within any specific dates etc meaning, I want to do something like:Campaign.first.users.where(?)
Now, when I query:
Campaign.first.users.all
I get list of all users within a certain campaign, but how I can I get only those users which have Campaign between specific date ranges only.
解决方案I want to get all users in a Campaign within any specific dates
ActiveRecord Association Extension
:#app/models/campaign.rb class Campaign < ActiveRecord::Base has_many :users, through: :view_logs do def between start_date, end_date where(created_at: start_date..end_date) end end end @campaign = Campaign.find x @users = @campaign.users.between(Date.today - 2, Date.today.day)
--
Update - it seems PGSQL requires a timestamp to evaluate dates:
@users = @campaign.users.between(Time.now.midnight - 2.days, Time.now.midnight)
The difference being that between
Date
&Time
objects.这篇关于查询通过导轨的has_many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!