Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord-如何知道另一个表上是否存在字段?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord-如何知道另一个表上是否存在字段?

Ruby on rails ActiveRecord-如何知道另一个表上是否存在字段?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,如果注册了具有相同邮件id的用户,我想将winners表中的状态更新为trues。我被活动唱片卡住了 schema.rb 表2 model/user.rb 如何使用active record进行此类查询?您可以尝试此方法,希望对您有所帮助 在model/user.rb中 after_create :update_status def update_status #update the corresponding winners row with status. winner = Win

如果注册了具有相同邮件id的用户,我想将winners表中的
状态更新为trues。我被活动唱片卡住了

schema.rb 表2 model/user.rb
如何使用active record进行此类查询?

您可以尝试此方法,希望对您有所帮助

model/user.rb中

after_create :update_status

def update_status
  #update the corresponding winners row with status.
  winner = Winner.where("email = ?", self.email).first
  unless winner.blank
    winner.status = true  # Or false
    winner.save! # Or If You want to skip validation then use winner.save(validate: false)
  end
end

如果您想在您的模型/user.rb:

after_create :update_status

def update_status
  Winner.where(:email => self.email).each do |winner|
    winner.status = true
    winner.save
  end
end

您可能会争辩说,基于用户注册更新赢家应该是控制器任务,而不是模型任务。我是在你的控制器中显式调用东西的朋友,而不是让它自动发生。不过这更符合个人口味,这应该很好。

你可以试试这个
def update_status winner=winner.where(“email=?”,self.email)。首先,除非winner.blank#winners表中的任何邮件winner.status=true#或false winner.save!end end
@AmitSharma,如果你觉得你的方法可以解决他的问题,请将其作为答案而不是评论。“注释部分中写入的方法不可读。@AmitSharma没错,如果您可以发布此答案部分,那就太好了。@user121212您的Winners表是否包含同一电子邮件地址的多个条目?”?
    after_create :update_status
    def update_status
     #if self[:email] = #any mail in winners table
      #update the corresponding winners row with status.
     if Winners.where(email = :email)
     update attributes

    end
after_create :update_status

def update_status
  #update the corresponding winners row with status.
  winner = Winner.where("email = ?", self.email).first
  unless winner.blank
    winner.status = true  # Or false
    winner.save! # Or If You want to skip validation then use winner.save(validate: false)
  end
end
after_create :update_status

def update_status
  Winner.where(:email => self.email).each do |winner|
    winner.status = true
    winner.save
  end
end