1

Association of model is

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

I want to get only those users of campaign where views_logs is created b/w specific dates.

Note: created_at query for views_logs not for Users creation

Sander de Jong
  • 351
  • 6
  • 18
Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133
  • can you put your user model with association here? – Vishal JAIN Feb 01 '16 at 11:20
  • Which model? kindly ellaborate please – Haseeb Ahmad Feb 01 '16 at 11:22
  • `user` model already mentioned – Vishal JAIN Feb 01 '16 at 11:22
  • @HaseebAhmad I told you to clarify the [question](http://stackoverflow.com/q/35125607/2767755). But you didn't or couldn't. Due to which, you are now asking the same question again in a separate post. So you can fix it in your first question. Prepare the question first, and then ask here. – Arup Rakshit Feb 01 '16 at 11:24
  • @ArupRakshit Sorry but I want to ask the same question there but now I doesnt want to delete it..that's why I post new question – Haseeb Ahmad Feb 01 '16 at 11:26
  • Association of Cmpaign and Users is through Viewslogs. I want to get only those users of campaigns where the Viewslogs.created_at is b/w specific dates – Haseeb Ahmad Feb 01 '16 at 11:28
  • I understood the fact. That's why I am asking, before post, make the draft clear as much as you can. Otherwise, someone will guess and answer you which might not be what you really asked. :/ – Arup Rakshit Feb 01 '16 at 11:29
  • If you understand question and have any solution please share it :) – Haseeb Ahmad Feb 01 '16 at 11:39
  • Didn't I just answer this exact question? – Richard Peck Feb 01 '16 at 12:15
  • I'm voting to close this question as off-topic because already been asked before, but deleted first – Richard Peck Feb 01 '16 at 12:15
  • No Sir that's different scenario.I want Association of Cmpaign and Users is through Viewslogs. I want to get only those users of campaigns where the Viewslogs.created_at is b/w specific dates – Haseeb Ahmad Feb 01 '16 at 12:16

1 Answers1

1
Campaign.joins(:users).where("views_logs.created_at = ?", my_date)

Joining Users like this will perform a SQL JOIN on your views_logs then a SQL JOIN on your users, which allows you to use a view_logs column in your WHERE condition.

Caillou
  • 1,451
  • 10
  • 19
  • Thanks but with I get Campaigns not Users :/ – Haseeb Ahmad Feb 01 '16 at 11:41
  • Then `Users.joins(:campaigns).where("views_logs.created_at = ?", my_date)` – Caillou Feb 01 '16 at 14:36
  • You seem to not be very fluent with ActiveRecord queries. I recommend reading this : http://guides.rubyonrails.org/active_record_querying.html – Caillou Feb 01 '16 at 14:37
  • Sorry but that's not woirking...it gives me user according to their created_at.. I want Association of Cmpaign and Users is through Viewslogs. I want to get only those users of campaigns where the Viewslogs.created_at is b/w specific dates – Haseeb Ahmad Feb 01 '16 at 14:47
  • Did you find what you needed in the documentation I linked ? – Caillou Feb 01 '16 at 16:49