Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 如何将第一次创建后的模型配置为只读_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何将第一次创建后的模型配置为只读

Ruby on rails 如何将第一次创建后的模型配置为只读,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我想知道是否可以配置我的模型,使其在创建后成为只读。我已尝试使用只读?方法: class Post < ApplicationRecord belongs_to :blog def readonly? true end end class Post # irb(主):002:0>p.update(名称:p.name.reverse) 回溯(最近一次呼叫最后一次): 1:来自(irb):2 ActiveRecord::ReadOnlyRecord(Post标记为只读)

我想知道是否可以配置我的模型,使其在创建后成为只读。我已尝试使用
只读?
方法:

class Post < ApplicationRecord
  belongs_to :blog

  def readonly?
    true
  end
end
class Post

但这也会阻止创建,这在我看来毫无意义。

有几种方法可以做到这一点,但如果您想在其他模型中重复使用某些东西,可以尝试下一种方法:

class ReadonlyValidator < ActiveModel::Validator
  def validate(record)
    return unless record.persisted?
    record.errors[:base] << "#{record.class.name} is readonly."
  end
end

class Post < ApplicationRecord
  validates_with ReadonlyValidator
end

有几种方法可以做到这一点,但如果您想在其他模型中重复使用某些东西,可以尝试下一种方法:

class ReadonlyValidator < ActiveModel::Validator
  def validate(record)
    return unless record.persisted?
    record.errors[:base] << "#{record.class.name} is readonly."
  end
end

class Post < ApplicationRecord
  validates_with ReadonlyValidator
end
class Post

irb(main):001:0>p=Post.create(名称:“Hello World”)
(0.3ms)开始
Post Create(1.6ms)在“posts”(“name”,“created_at”,“updated_at”)值($1,$2,$3)中插入返回“id”[[“name”,“Hello World”],[“created_at”,“2019-12-10 23:17:04.300537”],[“updated_at”,“2019-12-10 23:17:04.300537”]
(0.5ms)提交
=> #
irb(主):002:0>p.update(名称:p.name.reverse)
回溯(最近一次呼叫最后一次):
1:来自(irb):2
ActiveRecord::ReadOnlyRecord(Post标记为只读)
尽管Fran Martinez的回答很有意义,但如果您想使用验证来创建用户友好的响应,而不是拯救
ActiveRecord::ReadOnlyRecord
。它实际上可以归结为用例——更新记录是一个不应该发生的异常事件,还是由于用户错误而可能发生的事件?

class Post

irb(main):001:0>p=Post.create(名称:“Hello World”)
(0.3ms)开始
Post Create(1.6ms)在“posts”(“name”,“created_at”,“updated_at”)值($1,$2,$3)中插入返回“id”[[“name”,“Hello World”],[“created_at”,“2019-12-10 23:17:04.300537”],[“updated_at”,“2019-12-10 23:17:04.300537”]
(0.5ms)提交
=> #
irb(主):002:0>p.update(名称:p.name.reverse)
回溯(最近一次呼叫最后一次):
1:来自(irb):2
ActiveRecord::ReadOnlyRecord(Post标记为只读)
尽管Fran Martinez的回答很有意义,但如果您想使用验证来创建用户友好的响应,而不是拯救
ActiveRecord::ReadOnlyRecord
。它实际上可以归结为用例——更新记录是一个不应该发生的异常事件,还是由于用户错误而可能发生的事件

class Post < ApplicationRecord
  def readonly?
    persisted?
  end
end
irb(main):001:0> p = Post.create(name: 'Hello World')
   (0.3ms)  BEGIN
  Post Create (1.6ms)  INSERT INTO "posts" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["name", "Hello World"], ["created_at", "2019-12-10 23:17:04.300537"], ["updated_at", "2019-12-10 23:17:04.300537"]]
   (0.5ms)  COMMIT
=> #<Post id: 1, name: "Hello World", created_at: "2019-12-10 23:17:04", updated_at: "2019-12-10 23:17:04">
irb(main):002:0> p.update(name: p.name.reverse)
Traceback (most recent call last):
        1: from (irb):2
ActiveRecord::ReadOnlyRecord (Post is marked as readonly)