У меня есть модель пользователя
class User < ActiveRecord::Base
has_many :messages_received, :class_name=>"Message", :foreign_key=>'receiving_id', :conditions => {:message_type => "incoming"}
has_many :messages_sent, :class_name=>"Message", :foreign_key=>'sender_id', :conditions => {:message_type => "outgoing"}
end
Теперь я хочу получить всех пользователей, которые не отправляли сообщений за последние 15 дней, чтобы я мог уведомить их. Я попробовал это, но это не работает.
has_many :last_7_days_inactive_users, :class_name=>"Message", :foreign_key=>'receiving_id', :conditions =>{:messages_sent => ("created_at < ? " ,Time.now - 7.days)}
Как я могу изменить этот запрос?
Редактировать
User.last_7_days_inactive_users
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'users.messages_sent' in 'where clause': SELECT `users`.`id` AS t0_r0, `users`.`name` AS t0_r1, `users`.`email` AS t0_r2, `users`.`phone_number` AS t0_r3, `users`.`active` AS t0_r4, `users`.`created_at` AS t0_r5, `users`.`updated_at` AS t0_r6, `users`.`groups_count` AS t0_r7, `users`.`daily_sms_count` AS t0_r8, `users`.`new_user_sms_count` AS t0_r9, `users`.`source_type` AS t0_r10, `users`.`messages_count` AS t0_r11, `users`.`salt` AS t0_r12, `users`.`hashed_password` AS t0_r13, `users`.`verified_email` AS t0_r14, `users`.`email_verification_code` AS t0_r15, `users`.`auto_generated_password` AS t0_r16, `users`.`forgot_password_code_short` AS t0_r17, `users`.`forgot_password_code_long` AS t0_r18, `messages`.`id` AS t1_r0, `messages`.`body` AS t1_r1, `messages`.`sender_number` AS t1_r2, `messages`.`time_at` AS t1_r3, `messages`.`operator` AS t1_r4, `messages`.`circle` AS t1_r5, `messages`.`receiving_number` AS t1_r6, `messages`.`sender_id` AS t1_r7, `messages`.`system_phone_number_id` AS t1_r8, `messages`.`created_at` AS t1_r9, `messages`.`updated_at` AS t1_r10, `messages`.`message_type` AS t1_r11, `messages`.`parent_id` AS t1_r12, `messages`.`receiving_id` AS t1_r13, `messages`.`group_id` AS t1_r14, `messages`.`mp_hash` AS t1_r15, `messages`.`junk` AS t1_r16, `messages`.`command` AS t1_r17, `messages`.`message_size` AS t1_r18 FROM `users` LEFT OUTER JOIN `messages` ON `messages`.`sender_id` = `users`.`id` AND `messages`.`message_type` = 'incoming' WHERE `users`.`messages_sent` = 'messages_sent.created_at < DATE_SUB(UTC_TIMESTAMP(), INTERVAL 7 DAY)'