ruby - Ruby 和 ActiveRecord 初始化
问题描述
我正在研究 ActiveRecord 与 Ruby 的集成,并尝试在没有它的情况下做同样的事情,但一直失败。
这是我在没有 AR 的情况下所做的:
class User
include FirstLastAge
attr_reader :first_name, :last_name, :age
@@all = []
def self.all
@@all
end
def initialize(first_name = first.sample, last_name = last.sample, age = rand_age)
@first_name = first_name
@last_name = last_name
@age = age
self.class.all << self
end
end
# dat = SQLite3::Database.new('test.db')
# dat.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, age INTEGER);")
FirstLastAge 是一个具有 3 个方法的模块,#first 返回一个名字数组,#last 和 #rand_age。
通过这个设置,我可以 5000.times User.new 然后遍历它们并在数据库中创建一个条目。
所以现在我试图用 AR 做同样的事情,但一点运气都没有。
# User class
require_relative '../modules/names.rb'
class User < ActiveRecord::Base
include FirstLastAge
end
使用此架构:
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "age"
end
end
得到这个结果:
4] pry(main)> a = User.new
=> #<User:0x00007fb8bc1e16b8 id: nil, first_name: nil, last_name: nil, age: nil>
[5] pry(main)> a.first #Calling from the module
=> "Rossi"
[6] pry(main)> a.first #Calling from the module
=> "Xander"
[7] pry(main)> a.last #Calling from the module
=> "Patton"
[8] pry(main)> a.rand_age #Calling from the module
=> 11
[9] pry(main)> x = User.new(first_name: 'Alex', last_name: "Patton", age: 45)
=> #<User:0x00007fb8bdf1ccc8 id: nil, first_name: "Alex", last_name: "Patton", age: 45>
[10] pry(main)> y = User.new(first_name: first, last_name: "Patton", age: 45)
NameError: undefined local variable or method `first' for main:Object
from (pry):7:in `__pry__'
[11] pry(main)> y = User.new(first_name: self.first, last_name: "Patton", age: 45)
NoMethodError: undefined method `first' for main:Object
from (pry):8:in `__pry__'
[12] pry(main)> y = User.new(first_name: User.first, last_name: "Patton", age: 45)
=> #<User:0x00007fb8bc4a9bc0 id: nil, first_name: nil, last_name: "Patton", age: 45>
[13] pry(main)> y = User.new(first_name: User.first, last_name: "Patton", age: User.rand_age)
NoMethodError: undefined method `rand_age' for #<Class:0x00007fb8bc4a36d0>
from /Users/alex/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
我试过的最后一件事是有效的,但我不明白为什么,甚至不知道这是否是正确的方法是:
34] pry(main)>
[35] pry(main)>
[36] pry(main)> User.new.first
=> "Rihan"
[37] pry(main)> User.new.last
=> "Beck"
[38] pry(main)> User.new.rand_age
=> 24
[39] pry(main)> x = User.new(first_name: User.new.first, last_name: User.new.last, age: User.new.rand)
User.new.rand User.new.random_add User.new.random_iv User.new.random_number
User.new.rand_age User.new.random_bytes User.new.random_key
[39] pry(main)> x = User.new(first_name: User.new.first, last_name: User.new.last, age: User.new.rand_age)
=> #<User:0x00007fc664559588 id: nil, first_name: "Taylan", last_name: "Trevino", age: 74>
[40] pry(main)> y = User.new(first_name: User.new.first, last_name: User.new.last, age: User.new.rand_age)
=> #<User:0x00007fc6630dfd50 id: nil, first_name: "Sanaullah", last_name: "Koch", age: 56>
[41] pry(main)>
对正在发生的事情有任何想法,您将如何做到这一点?
谢谢你。
更新。
module FirstLastAge
def first
[array with 2000 names...to big to paste here].sample
end
def last
[array with 1000 names...to big to paste here].sample
end
def rand_age
rand(1..111)
end
end
我现在也放了 module_function :first, :last, :rand_age。所以我可以称它们为 FirstLastAge.rand_name。
解决方案
至于第一个结果,你得到 2 个错误的原因是:
- 您忘记在控制台中定义,
first
我猜您试图调用a.first
- 您尝试调用
User.rand_age
但它是即时方法而不是类方法,因此您不能通过这种方式调用它。
至于第二个结果,它与第一个结果略有不同。您使用了即时方法(User.new.first
和User.new.rand_age
)而不是使用类方法,所以是的,您的代码正在运行。
推荐阅读
- c# - 超时时间为 0 的 ManualResetEvent.WaitOne() 返回意外错误,尽管已设置事件
- typescript - 获取对象中的属性,但显示未定义
- sas - 我想将 sas 代码转换为 R。通常是 intnx 函数
- java - 如何将 http 入口点设置到我的 java 应用程序
- python - 有没有办法在经过一段时间后禁用更新 (UpdateView) 和删除 (DeleteView) 功能?
- random - 如何在 Common Lisp 中的 [0 ... 1.0] 中生成随机数
- java - java - 如何在java中从键盘填充整数的ArrayList?
- python - 图像未显示在 django 模板中,
- python-3.x - 有什么办法可以为我的 if else 循环添加更多条件?
- nestjs - NestJs 中的循环依赖问题