Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 - Fatal编程技术网

Ruby on rails 在视图中延迟加载关联时引发异常

Ruby on rails 在视图中延迟加载关联时引发异常,ruby-on-rails,ruby,Ruby On Rails,Ruby,使用熟悉的Rails示例关联,其中帖子有许多注释: 控制器 ... @posts = Post.find(:all) ... 查看 ... <% @posts.comments.each do |comment| %> ... 一旦我们开始渲染视图,有没有一种方法可以防止延迟加载关联?我希望有一种方法可以使它在关联丢失时抛出异常,但只有在我们进入视图时才会这样做 有任何插件可以做到这一点吗?这是一种几乎可以满足我要求的黑客行为: 内部配置/initializers/protect

使用熟悉的Rails示例关联,其中帖子有许多注释:

控制器

...
@posts = Post.find(:all)
...
查看

...
<% @posts.comments.each do |comment| %>
...
一旦我们开始渲染视图,有没有一种方法可以防止延迟加载关联?我希望有一种方法可以使它在关联丢失时抛出异常,但只有在我们进入视图时才会这样做


有任何插件可以做到这一点吗?

这是一种几乎可以满足我要求的黑客行为:

内部配置/initializers/protect_lazy_加载_在_view.rb中

class LazyLoadingPreventedInViewException < ActionView::TemplateError
  def initialize template_error
    super(template_error.instance_eval{@template}, template_error.instance_eval{@assigns}, template_error.original_exception)
  end
end
class ActionController::Base
  def render_with_lazy_load_prevention *args, &block
    ActiveRecord::Base.connection.disconnect!
    begin
      render_without_lazy_load_prevention *args, &block
    rescue ActionView::TemplateError => e
      if e.message['not connected']
        raise LazyLoadingPreventedInViewException.new(e)
      else
        raise e
      end
    end
    ActiveRecord::Base.connection.reconnect!
  end
  alias_method_chain :render, :lazy_load_prevention
end
类LazyLoadingPreventedViewExceptione 如果电子邮件[‘未连接’] 引发LazyLoadingPreventedViewException.new(e) 其他的 提高e 结束 结束 ActiveRecord::Base.connection.reconnect! 结束 别名\u方法\u链:渲染,:惰性\u加载\u预防 结束 这将在呈现视图时断开数据库的连接。任何延迟加载尝试都将导致包含“not connected”(未连接)消息的异常。我们截获了这个异常,并给它起了一个新名字“LazyLoadingPreventedViewException”,只是为了让它稍微不那么神秘


这绝对是一个黑客,而且也不是很好。可能会给毫无戒心的开发人员带来很大的混乱。如果我决定保留它,我肯定不会在生产中保留它。

从那时起,您找到更好的解决方案了吗?
class LazyLoadingPreventedInViewException < ActionView::TemplateError
  def initialize template_error
    super(template_error.instance_eval{@template}, template_error.instance_eval{@assigns}, template_error.original_exception)
  end
end
class ActionController::Base
  def render_with_lazy_load_prevention *args, &block
    ActiveRecord::Base.connection.disconnect!
    begin
      render_without_lazy_load_prevention *args, &block
    rescue ActionView::TemplateError => e
      if e.message['not connected']
        raise LazyLoadingPreventedInViewException.new(e)
      else
        raise e
      end
    end
    ActiveRecord::Base.connection.reconnect!
  end
  alias_method_chain :render, :lazy_load_prevention
end