我一直在用spring预加载程序实验rails 4.2.7中的一些内部Rails代码,并在irb中发现了堆栈转储的一些奇怪行为。
考虑以下代码
def run_block
yield
end
class Vehicle < ActiveRecord::Base
def self.instantiate(*args)
puts caller_locations
super
end
def self.test_stack
run_block {Vehicle.all}
end
end
注意,我正在扩展实例化函数()的功能,以打印出调用堆栈。
当
我可以取消对Bar ()的定义,但是如何取消Foo::Bar的定义呢?
irb(main):266:0> Object.send :remove_const, :ActiveRecord::Base
TypeError: :ActiveRecord is not a class/module
irb(main):267:0> Object.send :remove_const, :"ActiveRecord::Base"
NameError: `ActiveRecord::Base' is not allowed as a constant name
i
我从事模拟工作,经常需要多个数据生成器,它们在结构上是相同的,但具有不同的参数化。我试图使用C扩展编写一个gem来实现一个工厂方法,该方法根据提供的参数化来确定和创建适当类型的枚举器。以下不是我的实际代码,而是作为一个最低限度的可重复示例来说明我感到困惑的行为:
#include "ruby.h"
VALUE rb_mTest = Qnil;
VALUE rb_cTest = Qnil;
static VALUE super_initialize(VALUE self) {
return self;
}
static VALUE rand_enum(VALUE sel
我使用的是4,我有这样的用户模型:
require 'uuid'
UUID.state_file = false
UUID.generator.next_sequence
class User < ActiveRecord::Base
attr_accessor :email, :password
has_many :entries
after_initialize do |user|
user.entry_hash = UUID.new.generate
end
end
它基于以下DB迁移:
class CreateUsers
我是rails和ActiveRecord的新手,我不明白为什么rails不能为下面的模型设置生成User.build_company方法:
class User < ActiveRecord::Base
has_one :found_company
has_one :company, through: :found_company
end
class FoundCompany < ActiveRecord::Base
belongs_to :user
belongs_to :company
end
class Company < ActiveRecord
我有两个模型在一个具有载波的rails项目中使用相同的上传器。
class Brand < ActiveRecord::Base
mount :avatar, AvatarUploader
end
class Profile < ActiveRecord::Base
mount :avatar, AvatarUploader
end
然后当我试图复制一个图像到另一个模型
profile.avatar = brand.avatar
profile.save!
它在以下堆栈跟踪中失败
undefined method `content_length' for
mysql
Rails 3.2.18
在控制台中:
irb(main):056:0> Order.find(964179).delivery_target
=> Tue, 14 Jul 2015 13:13:21 CDT -05:00
irb(main):057:0> ActiveRecord::Base.connection.select("select o.delivery_target from orders o where o.id=964179")
(1.6ms) select o.delivery_target from orders o
我使用find_by_sql,不到一秒钟,它就会给我一个响应:
Report.find_by_sql("SELECT min(reports.time) FROM reports WHERE unit_id = '#{Unit.find(3007).id}' AND driver_id = '#{Driver.find(2).id}' AND time >= '#{beginning}' AND time <= '#{ending}'")
Unit Load (111.1ms) SELECT