【问题标题】:How to show user.name associated with timesheets?如何显示与时间表关联的用户名?
【发布时间】:2013-08-13 03:17:11
【问题描述】:

我的应用在时间表索引中显示 user.name 时遇到问题(显示所有时间表,而不仅仅是当前用户时间表)

用户模型:

# Table name: timesheets
#
#  id         :integer         not null, primary key
#  user_id    :integer
#  starting   :datetime
#  ending     :datetime
#  approved   :boolean
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password
  has_many :timesheets
  ....
end

时间表模型(标准 user_id 是外键)

# Table name: timesheets
#
#  id         :integer         not null, primary key
#  user_id    :integer
#  starting   :datetime 
#  ending     :datetime
#  approved   :boolean
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class Timesheet < ActiveRecord::Base
  attr_accessible :starting, :ending
  belongs_to :user

  validates :user_id, presence: true
  validates :starting, presence: true
  validates :ending, presence: true
  validate :end_after_start
...
end

我的时间表控制器的索引定义:

def index
  @timesheets = Timesheet.paginate(page: params[:page], per_page: 10)
end

最后是显示 user_id 的 index.html.erb 文件,但我想要的是用户名。请记住,这是由管理员批准时间表,因此不会是登录用户,而是希望查看所有时间表并批准他们想要的时间表的管理员。

<ul class="timesheets">
<% @timesheets.each do |timesheet| %>
    <NOBR>
    <li>
          <%= timesheet.user_id %>
          <%= timesheet.starting.strftime("[ %^a %^b %e,  %Y - %l:%M %p ] - ") %><%= timesheet.ending.strftime("[ %^a %^b %e,  %Y - %l:%M %p ]") %> <%= (timesheet.ending - timesheet.starting)/3600 %> Hours
          <% if current_user.admin? %>
                |
              <% if timesheet.approved? %>
                 <%= link_to "Un-Approve", { action: :unapprove, id: timesheet.id }, method: :put %>
              <% else %>
                 <%= link_to "Approve", { action: :approve, id: timesheet.id }, method: :put %>
              <% end %> 
         <% end %>
        </li>
    </NOBR>
<% end %>
</ul>

<%= will_paginate %>

非常感谢您的帮助,这是我提出的第一个问题,所以我希望我遵循了所有正确的协议。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    根据“RAILS ANTIPATTERNS - Ruby on Rails 重构最佳实践

    Ruby on Rails 使您可以轻松地在对象之间的关系之间导航,因此可以轻松地深入了解相关对象并跨相关对象进行深入研究。根据 Rails 中的得墨忒耳定律,在对象关系之间导航时“只使用一个点”。 例如,@category.product.name 违反了得墨忒耳法则,但 @category.product_name 没有。

    因此,获取时间表的用户名的最佳方法是使用

    timesheet.user_name
    

    代替

    timesheet.user.name
    

    为此,只需将具有所需属性的委托方法添加到您的 Timesheet 模型中,如下所示

    delegate :name, :email, to: :user, :prefix => true
    

    所以你的模型将是

    class Timesheet < ActiveRecord::Base
      attr_accessible :starting, :ending
      belongs_to :user
    
      validates :user_id, presence: true
      validates :starting, presence: true
      validates :ending, presence: true
      validate :end_after_start
    
      delegate :name, :email, to: :user, :prefix => true
    
      ...
    
    end
    

    现在,通过调用获取视图文件中的用户名

    timesheet.user_name
    

    这样你也可以得到如下的用户邮箱

    timesheet.user_email
    

    【讨论】:

    • 只是为了让我更好地理解这一点,如果 .符号有效吗?通过 .符号?
    • 不,您可能正在做一些比以前的方法更好、更安全的事情。在以前的方法中,您违反了封装。应用得墨忒耳定律的主要好处之一是通过封装实现的低耦合。有关得墨忒耳定律的详细解释,请点击链接haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx
    【解决方案2】:

    如果你与用户模型有关联,那么你可以通过调用来获取用户模型

    timesheet.user
    

    在这个问题的情况下

    <ul class="timesheets">
    <% @timesheets.each do |timesheet| %>
        <NOBR>
        <li>
              <%= timesheet.user.name %>
              <%= timesheet.starting.strftime("[ %^a %^b %e,  %Y - %l:%M %p ] - ") %><%= timesheet.ending.strftime("[ %^a %^b %e,  %Y - %l:%M %p ]") %> <%= (timesheet.ending - timesheet.starting)/3600 %> Hours
              <% if current_user.admin? %>
                    |
                  <% if timesheet.approved? %>
                     <%= link_to "Un-Approve", { action: :unapprove, id: timesheet.id }, method: :put %>
                  <% else %>
                     <%= link_to "Approve", { action: :approve, id: timesheet.id }, method: :put %>
                  <% end %> 
             <% end %>
            </li>
        </NOBR>
    <% end %>
    </ul>
    
    <%= will_paginate %>
    

    【讨论】:

    • 谢谢你,它工作得很好!!!我在尝试 timesheet_user.name,现在感觉很愚蠢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多