ruby on rails 3 - Activerecord adds additional conditions in query -
for example doing this:
platforminformer.where(1)
and result query via
platforminformer.where(1).to_sql
is
select `platform_informers`.* `platform_informers` `platform_informers`.`platform` = 'android' , `platform_informers`.`email` = 'voldemar@klops.ru' , (1)
i didn't ask add email , platform fields in clause!
this problem causes when executing code inside platforinformer model methods. default scope doesn't set. root of evil?
rails 3.2.13
update:
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
problem in using first_or_create method, created virtual scope email , platform attributes.
Comments
Post a Comment