0

Association is

class Campaign
  has_many :views_logs
  has_many :users, through: :views_logs
end

I want to get all users in a Campaign 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.

aliibrahim
  • 1,695
  • 1
  • 12
  • 18
Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133

3 Answers3

2

Simply define a scope in User like this:

class User < ActiveRecord::Base
  # Your current code
  scope :during_campaign, -> (start, end) { 
       joins(view_logs: :campaign).where('campaigns.created_at >= ? AND campaigns.created_at <= ?', start, end)
  }

end

Then use it:

# User from yesterday compaign for eg
User.during_campaign(1.day.ago, Time.now)

Make it generic & pretty always!

Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
1

Have you tried doing:

Campaign.first.users.where("campaigns.created_at > #{start_date} AND campaigns.created_at < #{end_date}")
aliibrahim
  • 1,695
  • 1
  • 12
  • 18
1

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.

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • PG::UndefinedFunction: ERROR: operator does not exist: timestamp without time zone <= integer LINE 1: ...ogs"."campaign_id" = $1 AND ("users"."created_at" BETWEEN '2... – Haseeb Ahmad Feb 01 '16 at 12:18
  • Can you post the full query after `BETWEEN 2` ? Thanks! – Richard Peck Feb 01 '16 at 12:44
  • Query is Campaign.first.users.between(Date.today - 2, Date.today.day) – Haseeb Ahmad Feb 01 '16 at 12:46
  • Thanks - when you posted `RROR: operator does not exist: timestamp without time zone <= integer LINE 1: ...ogs"."campaign_id" = $1 AND ("users"."created_at" BETWEEN '2` -- can you show me the last part of the reponse? – Richard Peck Feb 01 '16 at 12:47
  • THanks - PG::UndefinedFunction: ERROR: operator does not exist: timestamp without time zone <= integer LINE 1: ...ogs"."campaign_id" = $1 AND ("users"."created_at" BETWEEN '2.. ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. : SELECT "users".* FROM "users" INNER JOIN "views_logs" ON "users"."id" = "views_logs"."user_id" WHERE "users"."deleted_at" IS NULL AND "views_logs"."deleted_at" IS NULL AND "views_logs"."campaign_id" = $1 AND ("users"."created_at" BETWEEN '2016-01-30' AND 1) – Haseeb Ahmad Feb 01 '16 at 12:51
  • 1
    Looks like we have to send the values as a [timestamp](http://stackoverflow.com/a/25930087/1143732), rather than a string as we have now. Updated – Richard Peck Feb 01 '16 at 12:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102342/discussion-between-haseeb-ahmad-and-rich-peck). – Haseeb Ahmad Feb 02 '16 at 10:26