Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 在活动记录中创建之前

Ruby on rails 在活动记录中创建之前,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我想在为我的所有模块创建之前设置一个 我一直在尝试的是: module ActiveRecord module UserMonitor require 'securerandom' before_create :attach_uuid def attach_uuid self.uuid = SecureRandom.uuid.gsub("-","") end end end 这似乎不起作用。 如果我进入每个模块并将其添加到其中,它就会工

我想在为我的所有模块创建之前设置一个

我一直在尝试的是:

module ActiveRecord
  module UserMonitor
    require 'securerandom'

    before_create :attach_uuid
      def attach_uuid
      self.uuid = SecureRandom.uuid.gsub("-","")
    end
  end
end
这似乎不起作用。 如果我进入每个模块并将其添加到其中,它就会工作,但我想在全球范围内进行

关于我如何以这种方式实现这一点,有什么想法或想法吗?我知道我可以在触发器中这样做,但我不想走这条路,我希望避免碰到每个模块/类,以防我需要更改某些内容

当前使用Ruby 1.9.3的应用程序在我将来更改代码之前无法升级


谢谢

如果您在ActiveRecord模块中定义了
attach\u uuid
,您就不能在每个控制器顶部的\u create:attach\u uuid之前调用
?这是干的

是否有可以将其添加到的UserMonitor控制器

class UserMonitor < ActiveRecord::Base
  before_create :attach_uuid
end
class UserMonitor
如果您在ActiveRecord模块中定义了
attach\u uuid
,您就不能在每个控制器顶部的\u create:attach\u uuid
之前调用
?这是干的

是否有可以将其添加到的UserMonitor控制器

class UserMonitor < ActiveRecord::Base
  before_create :attach_uuid
end
class UserMonitor
如果您在ActiveRecord模块中定义了
attach\u uuid
,您就不能在每个控制器顶部的\u create:attach\u uuid
之前调用
?这是干的

是否有可以将其添加到的UserMonitor控制器

class UserMonitor < ActiveRecord::Base
  before_create :attach_uuid
end
class UserMonitor
如果您在ActiveRecord模块中定义了
attach\u uuid
,您就不能在每个控制器顶部的\u create:attach\u uuid
之前调用
?这是干的

是否有可以将其添加到的UserMonitor控制器

class UserMonitor < ActiveRecord::Base
  before_create :attach_uuid
end
class UserMonitor
我使用的另一种解决方案是将UUID的逻辑放在自己的模块中,您可以将其包括在内。我已经在AR中添加了一些(类)方法,比如
set\u default\u if
,所以这对我来说是个好地方

module MyRecordExt
    def self.included base
        base.extend ClassMethods   # in my case some other stuff 
        base.before_create :attach_uuid # now add the UUID 
    end

    def attach_uuid
        begin
            self.uuid = SecureRandom.uuid
        rescue
            # do the "why dont we have a UUID filed?" here
        end
    end


    # some other things not needed for add_uuid
    module ClassMethods
        include MySpecialBase # just an eg.   

        def default_for_if(...)
            ...
        end
    end
end         
然后

class Articel < ActiveRecord::Base
    include MyRecordExt
    ...
end 
class-Articel

总的来说,我避免为所有模型做一些修改AR库的事情——我第一次在所有模型中添加UUID时经历了不好的经历,然后与Desive GEMs模型一起崩溃

我使用的另一个解决方案是将UUID的逻辑放在自己的模块中,您可以将其包括在内。我已经在AR中添加了一些(类)方法,比如
set\u default\u if
,所以这对我来说是个好地方

module MyRecordExt
    def self.included base
        base.extend ClassMethods   # in my case some other stuff 
        base.before_create :attach_uuid # now add the UUID 
    end

    def attach_uuid
        begin
            self.uuid = SecureRandom.uuid
        rescue
            # do the "why dont we have a UUID filed?" here
        end
    end


    # some other things not needed for add_uuid
    module ClassMethods
        include MySpecialBase # just an eg.   

        def default_for_if(...)
            ...
        end
    end
end         
然后

class Articel < ActiveRecord::Base
    include MyRecordExt
    ...
end 
class-Articel

总的来说,我避免为所有模型做一些修改AR库的事情——我第一次在所有模型中添加UUID时经历了不好的经历,然后与Desive GEMs模型一起崩溃

我使用的另一个解决方案是将UUID的逻辑放在自己的模块中,您可以将其包括在内。我已经在AR中添加了一些(类)方法,比如
set\u default\u if
,所以这对我来说是个好地方

module MyRecordExt
    def self.included base
        base.extend ClassMethods   # in my case some other stuff 
        base.before_create :attach_uuid # now add the UUID 
    end

    def attach_uuid
        begin
            self.uuid = SecureRandom.uuid
        rescue
            # do the "why dont we have a UUID filed?" here
        end
    end


    # some other things not needed for add_uuid
    module ClassMethods
        include MySpecialBase # just an eg.   

        def default_for_if(...)
            ...
        end
    end
end         
然后

class Articel < ActiveRecord::Base
    include MyRecordExt
    ...
end 
class-Articel

总的来说,我避免为所有模型做一些修改AR库的事情——我第一次在所有模型中添加UUID时经历了不好的经历,然后与Desive GEMs模型一起崩溃

我使用的另一个解决方案是将UUID的逻辑放在自己的模块中,您可以将其包括在内。我已经在AR中添加了一些(类)方法,比如
set\u default\u if
,所以这对我来说是个好地方

module MyRecordExt
    def self.included base
        base.extend ClassMethods   # in my case some other stuff 
        base.before_create :attach_uuid # now add the UUID 
    end

    def attach_uuid
        begin
            self.uuid = SecureRandom.uuid
        rescue
            # do the "why dont we have a UUID filed?" here
        end
    end


    # some other things not needed for add_uuid
    module ClassMethods
        include MySpecialBase # just an eg.   

        def default_for_if(...)
            ...
        end
    end
end         
然后

class Articel < ActiveRecord::Base
    include MyRecordExt
    ...
end 
class-Articel

总的来说,我避免为所有模型做一些修改AR库的事情——我第一次在所有模型中添加UUID时经历了不好的经历,然后与Desive GEMs模型一起崩溃

您看到服务器日志中有任何错误吗?我正在尝试在本地运行服务器。这里是尝试运行服务器时,将这些行添加到usermonitor文件/home/userm/workspace/application-O/vendor/plugins/usermonitor/lib/usermonitor.rb:5:in
:undefined method
before\u create'forActiveRecord::UserMonitor:模块(NoMethodError)您看到服务器日志中有任何错误吗?我正在尝试在本地运行服务器。这里是尝试运行服务器时,将这些行添加到usermonitor文件/home/userm/workspace/application-O/vendor/plugins/usermonitor/lib/usermonitor.rb:5:in
:undefined method
before\u create'forActiveRecord::UserMonitor:模块(NoMethodError)您看到服务器日志中有任何错误吗?我正在尝试在本地运行服务器。这里是尝试运行服务器时,将这些行添加到usermonitor文件/home/userm/workspace/application-O/vendor/plugins/usermonitor/lib/usermonitor.rb:5:in
:undefined method
before\u create'forActiveRecord::UserMonitor:模块(NoMethodError)您看到服务器日志中有任何错误吗?我正在尝试在本地运行服务器。这里是尝试运行服务器时,将这些行添加到usermonitor文件/home/userm/workspace/application-O/vendor/plugins/usermonitor/lib/usermonitor.rb:5:in
:undefined method
before\u create'for是的,我可以。但我希望尽可能避免在每个控制器中添加该行。如果应用程序中的每个模型都重复使用相同的行,那就不是干的。我认为在每个模块中重新定义
attach\uuid
是重复的,但在每个模块中调用它似乎是必要的。OP已经将大部分代码移到了一个公共文件中。是的,我可以。但如果可以的话,我希望避免在每个控制器上添加该行。如果应用程序中的每个模型都重复相同的行,这并不是干的。我认为在中重新定义
attach\uuid