Ruby和ActiveRecord初始化

时间:2019-03-03 15:06:01

标签: ruby activerecord

我正在研究与Ruby的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的名字的数组。

通过此设置,我可以对User.new进行5000次遍历,然后对其进行遍历并在数据库中创建一个条目。

所以现在我正尝试对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。

1 个答案:

答案 0 :(得分:0)

对于第一个结果,出现2个错误的原因是:

  1. 您忘了在控制台中定义first,我想您是在尝试调用a.first
  2. 您尝试调用User.rand_age,但这是一种即时方法,而不是类方法,因此您无法通过这种方式调用它。

对于第二个结果,它与第一个结果略有不同。您使用即时方法(User.new.firstUser.new.rand_age)而不是使用类方法,所以是的,您的代码正在运行。