我有一个查询,以找到一定的标准属性。我在查询中包含了Users表,这样我就可以访问属性所有者(user_id),并向他们发送有关该属性的电子邮件。
@testproperties.users.each do |property|
UserMailer.with(property: property, user: property.user).check_listing.deliver
property.update_columns(property_reminder_date: Time.now )
end
这工作正常,除了因为我循环通过属性,如果用户有一个以上的属性,他们将收到X数量的电子邮件。我想按用户捆绑这个。
因此,如果一个用户有2个属性,我想发送邮件的用户和他们的多个属性。他们将收到一封电子邮件,而不是两封。并且电子邮件视图将被馈送一系列属性。
但我不知道该怎么做。我需要遍历用户,并将他们的属性连接到他们。
编辑:
如果我做一个类似这样的查询:
@testusers = User.joins(:properties)
.where("properties.updated_at < :date", date: 30.days.ago)
.where("properties.property_reminder_date < :date OR properties.property_reminder_date IS NULL", date: 30.days.ago)
.where('properties.id NOT IN (SELECT DISTINCT(property_id) FROM transactions)')
这将给予我需要发送电子邮件的用户,但是,访问每个用户的属性显示所有属性,而不是我需要的基于我的SQL查询。
再次编辑:
我能以一种非常混乱的方式实现我想要的:
@testusers = User.joins(:properties)
.where("properties.updated_at < :date", date: 30.days.ago)
.where("properties.property_reminder_date < :date OR properties.property_reminder_date IS NULL", date: 30.days.ago)
.where('properties.id NOT IN (SELECT DISTINCT(property_id) FROM transactions)')
@testusers = @testusers.uniq
@testusers.each do |user|
propertyList = user.properties.active.has_not_updated
UserMailer.with(properties: propertyList, user: user).check_listing.deliver
propertyList.each do |property|
property.update_columns(property_reminder_date: Time.now )
end
end
物业型号:
...
scope :active, -> { where('expires_at >= ?', Time.now) }
scope :has_not_updated, -> {active.where("updated_at < :date", date: 30.days.ago).where("property_reminder_date < :date OR property_reminder_date IS NULL", date: 30.days.ago).where('id NOT IN (SELECT DISTINCT(property_id) FROM transactions)') }
1条答案
按热度按时间e4eetjau1#
您可以执行以下操作:
在每次迭代中,您将拥有user和user属性数组。