I have
class Campaign < ApplicationRecord
has_many :clicks
has_many :cpas
has_many :ftds
has_many :signups
end
and
class Click < ApplicationRecord
belongs_to :campaign
end
class Cpa < ApplicationRecord
belongs_to :campaign
end
class Ftd < ApplicationRecord
belongs_to :campaign
end
class Signup < ApplicationRecord
belongs_to :campaign
end
The models clicks, cpas, ftds, signups are kind of events that happens throughout the time to the campaign model. I want to create a reports page the list all the campaign and show on each line the events (clicks, cpas, ftds, signups) that happened(created_at) on given time(on this month).
On my attemps I get the campaign with events on the month, but the data listed on the lines brings events that happened on other month I chose, but also others months.
def self.filter_by_date(start_date = Time.now.beginning_of_month, end_date = Time.now.end_of_month)
joins(:clicks).where(clicks: {created_at: start_date..end_date}) +
joins(:ftds).where(ftds: {created_at: start_date..end_date}) +
joins(:cpas).where(cpas: {created_at: start_date..end_date}) +
joins(:signups).where(signups: {created_at: start_date..end_date})
end
Sorry for the not so good english, I'm Brazilian.