例如我正在做这样的事情:
PlatformInformer.where(1)
并通过
查询结果PlatformInformer.where(1).to_sql
是
SELECT `platform_informers`.* FROM `platform_informers` WHERE `platform_informers`.`platform` = 'android' AND `platform_informers`.`email` = 'voldemar@klops.ru' AND (1)
我没有要求在where子句中添加电子邮件和平台字段!
当我在PlatforInformer模型方法中执行代码时,会出现此问题。默认范围未设置。邪恶的根源是什么?
Rails 3.2.13
更新:
class PlatformInformer < ActiveRecord::Base
include HasUniqueGenerator
attr_accessible :email, :platform,:secret_code,:activated,:invitation_sent
before_create :init_secret_code
PLATFORMS = %w(windows macos android ios)
def self.PLATFORMS
PLATFORMS
end
validates :platform, :presence => true,:inclusion => { :in =>PLATFORMS }
validates :email, :presence => true, :email => true
validates_uniqueness_of :email, scope: :platform
scope :confirmed, Proc.new { where(:activated => true) }
def several_platforms?
PlatformInformer.confirmed.find_all_by_email(self.email).count > 0
end
def send_confirmation
if already_subscribed?
activate!
else
PlatformInformerMailer.inform_me(self.id).deliver
end
end
def activate!
PlatformInformer.where(:email=>self.email).update_all(:activated=>true)
end
private
def init_secret_code
gen_unique_code :secret_code, 16
end
def already_subscribed?
PlatformInformer.confirmed.where(email: self.email).any?
end
end
答案 0 :(得分:0)
问题在于使用first_or_create方法,该方法使用电子邮件和平台属性创建了虚拟范围。