问题描述:

create_table :tasks do |t|
  t.string :name
  t.time :interval
  t.text :selectIf
  t.text :url
  t.string :status, :default => "ready"
  t.integer :mail_succeeds_count, :default => 0
  t.integer :mail_faileds_count, :default => 0
  t.timestamps
end

class Task < ActiveRecord::Base

……

   def send_mail

       ……

          status = "stop"

       ……

   end

end

在 Task 中 给 status 属性赋值,但结果还是没有设置成功。

问题原因:

tasks 表中 status 属性,

  在读 status 时,Task会先找定义,如果没有找到(回调missing_mehod方法) 才去找数据库中的模式。

  但ruby 中 对一个对象赋值,如果这个对象没有存在, 就直接当成生成一个。

  这里 status = "stop" 中的 stop被当成那个了 status局部变量。

 

解决办法:

   使用 self.status = "stop" 方式

    这样 status 必定当成了Task对象的方法,如果方法没有找到就会(回调 missing_method) 找到数据库模式。

总结:

    ActiveRecord 中的数据库模式 属性的映射,是通过 missing_method 方法来实现的,

    所有的ActiveRecord 数据库属性 都是个 reader 方法。

相关文章:

  • 2022-03-04
  • 2022-02-19
  • 2021-10-25
  • 2021-11-05
  • 2023-02-20
  • 2021-04-08
  • 2021-12-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2021-09-30
  • 2021-07-04
  • 2021-07-30
相关资源
相似解决方案