【问题标题】:How to keep ActiveRecord association DRY?如何保持 ActiveRecord 关联 DRY?
【发布时间】:2008-10-16 21:45:56
【问题描述】:

我在尝试让我的应用程序中的 AR 查找器保持干燥时遇到问题。我创建了一个博客应用程序,当用户第一次查看它时,它会获取博客的所有相关页面、帖子、链接、标签和类别。博客控制器的示例显示操作如下所示:

def show
    #find blog by user name
    @user= User.find_by_login(params[:id])
    @blog= @user.blog
    @posts = Post.status("publish",@user).find(:all, :order => "created_at DESC")
    @tags = @user.tags
    @pages = Page.status("publish",@user).find(:all, :order => "created_at DESC")
    @links = @user.links.public_link.find(:all, :order => 'created_at DESC')
    @archives = @posts.group_by(&:month)
    @categories = @user.categories.group_by(&:name)
    session[:found_user]=@user.login
    render :layout=>false
  end

正如你所看到的,它不是很干燥,因为在控制器中还有其他操作会调用相同的实例变量,例如 @tags 等。

我怎样才能使这个更干燥?我尝试将它移动到博客模型中,但我仍然需要在控制器中调用各种实例变量,例如 @tags 等。

有没有办法在第一次调用博客时存储所有这些变量并在控制器和操作中重复使用它们?

感谢您的任何建议。我正在使用 Rails 2.1

【问题讨论】:

  • 哇,我觉得你的 shiftkey 有点问题。
  • 说真的。你的问题让我耳朵疼。

标签: mysql activerecord dry


【解决方案1】:

我在某处的博客上读到,只是简单地将过滤器(或在控制器方法中加载各种数据)替换为辅助方法。像这样的:

class BlogsController < ApplicationController
  def show
    session[:found_user]=@user.login
    render :layout=>false
  end

  helper_method :user, :blog, :posts, :tags, :pages, :links, :archives, :categories

  protected
  def user
    @user ||= User.find_by_login(params[:id])
  end

  def blog
    @blog ||= user.blog
  end

  def posts
    @posts ||= Post.status("publish", user).find(:all, :order => "created_at DESC")
  end

  def tags
    @tags ||= user.tags
  end

  def pages
    @pages ||= Page.status("publish", user).find(:all, :order => "created_at DESC")
  end

  def links
    @links ||= user.links.public_link.find(:all, :order => 'created_at DESC')
  end

  def archives
    @archives ||= posts.group_by(&:month)
  end

  def categories
    @categories ||= user.categories.group_by(&:name)
  end
end

## app/views/blogs/show.html.erb
<p>Name: <%=h user.name %></p>
<h3><%= posts.length %> Posts</h3>
<% posts.each do |post| %>
  ...
<% end %>
<ul>
  <% categories.each do |category| %>
    <li><%=h category %></li>
  <% end %>
</ul>

看看如何在视图中简单地调用数据库。此解决方案的一个优点是未调用的辅助方法不会占用操作时间。

如有必要,将辅助方法抽象为一个模块,并将该模块包含在 ApplicationController 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2019-10-20
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多