<commit_before>class Member < ActiveRecord::Base
  attr_accessible :name, :email
  has_many :subscribes
  has_many :unsubscribes
  has_many :sent_emails

  validates :email, :presence => true, :uniqueness => true
  validates :name, :presence => true

  def self.random_and_not_recently_contacted
	  uncontacted_members = Member.joins("LEFT JOIN sent_emails ON (members.id = sent_emails.member_id AND sent_emails.created_at > now() - interval '1 week') WHERE sent_emails.member_id is null")
	  subscribe_dates = Subscribe.group(:member_id).maximum(:created_at)
	  unsubscribe_dates = Unsubscribe.group(:member_id).maximum(:created_at)
    subscribers = uncontacted_members.select {|m| active_subscription?(subscribe_dates[m.id], unsubscribe_dates[m.id])}
	  subscribers.sample
  end

  def self.active_subscription?(subscribe_date, unsubscribe_date)
 		return true if unsubscribe_date.nil?<commit_msg>reverting to SQL join - ActiveRecord is crazy slow here for some reason<commit_after>class Member < ActiveRecord::Base
  attr_accessible :name, :email
  has_many :subscribes
  has_many :unsubscribes
  has_many :sent_emails

  validates :email, :presence => true, :uniqueness => true
  validates :name, :presence => true

  def self.random_and_not_recently_contacted
	  uncontacted_members = Member.connection.execute("SELECT members.id FROM members LEFT JOIN sent_emails ON (members.id = sent_emails.member_id AND sent_emails.created_at > now() - interval '1 week') WHERE sent_emails.member_id is null").to_a
	  subscribe_dates = Subscribe.group(:member_id).maximum(:created_at)
	  unsubscribe_dates = Unsubscribe.group(:member_id).maximum(:created_at)
    subscribed_members = uncontacted_members.select {|m| active_subscription?(subscribe_dates[m['id'].to_i], unsubscribe_dates[m['id'].to_i])}

	  return nil if subscribed_members.empty?
	  Member.find(subscribed_members.sample['id'])
  end

  def self.active_subscription?(subscribe_date, unsubscribe_date)
 		return true if unsubscribe_date.nil?
{'fraction_non_alphanumeric': 0.0985, 'fraction_numerical': 0.001, 'mean_word_length': 6.070671378091872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6396232', 'n_tokens_mistral': 660, 'n_tokens_neox': 635, 'n_words': 136}