Ruby on rails 为什么;创造&引用;而不是创造?
我正在努力学习RSpec,学习他们的博士学位。但是,他们的示例使用函数create!那个鲁比明不认识,我也不知道 它是如何工作的?它是在ActiveRecord基类中实现的吗 下面是我所说的示例:Ruby on rails 为什么;创造&引用;而不是创造?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在努力学习RSpec,学习他们的博士学位。但是,他们的示例使用函数create!那个鲁比明不认识,我也不知道 它是如何工作的?它是在ActiveRecord基类中实现的吗 下面是我所说的示例: require "rails_helper" RSpec.describe User, :type => :model do it "orders by last name" do lindeman = User.create!(first_name: "Andy", last_n
require "rails_helper"
RSpec.describe User, :type => :model do
it "orders by last name" do
lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")
expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
end
end
是的,它是一种ActiveRecord方法
create
将返回true
或false
。
创建如果操作失败,代码>将引发异常。它在文档中:!那是不对的User.create
将返回一个用户实例,该实例可能存在,也可能不存在。如果验证失败,但它始终返回一个用户实例<代码>User.create代码>将返回用户实例或引发异常。