Rails4:如何仅显示一次值

时间:2015-09-22 20:57:47

标签: ruby-on-rails ruby ruby-on-rails-4

我想只显示一次房间名称。 总结如下;

文章表

id day start_time title
2  1   09:00      Math
4  2   10:00      English
5  1   11:00      Science
8  3   12:00      Physics
9  2   13:00      Music

房间桌

id day name
3  1   classA
6  2   classB
7  3   classC

我想在视图中显示如下数据;

Day1
09:00 Math
11:00 Science
Room classA

Day2
10:00 English
13:00 Music
Room classB

请告诉我如何显示上述值。 每次都会在下面的代码中显示房间名称。

查看代码

<div class="row">
  <% @article.group_by(&:day).each do |day, articles| %>
    <h3>Day <%= day %></h3>

    <% articles.each do |a| %>
      <%= a.start_time %>
      <% a.category %>
      <%= a.contents %><br>
      <%= a.matching_detail.try(:name) %><br> #I'd like to display only one time
    <% end %>

  <% end %>
</div>

型号代码

class Article < ActiveRecord::Base
    belongs_to :school
    has_many :rooms, through: :school
    default_scope -> { order(day: :asc, start_time: :asc) }

    def matching_detail
      rooms.find_by_day day
    end
end

class Room < ActiveRecord::Base
    belongs_to :school
    belongs_to :article
end

模式

ActiveRecord::Schema.define(version: 20150999999999) do

  create_table "rooms", force: true do |t|
    t.integer  "school_id"
    t.integer  "day"
    t.string   "name"
    t.string   "detail"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "articles", force: true do |t|
    t.integer  "school_id"
    t.integer  "day"
    t.string   "start_time"
    t.string   "end_time"
    t.integer  "category"
    t.string   "title"
    t.string   "contents"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "schools", force: true do |t|
    t.string   "title"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

2 个答案:

答案 0 :(得分:2)

我通过以下方式重新创建了rails项目:

  1. rails new SchoolScheduler
  2. 从您的问题中添加了架构文件
  3. rails db:schema:load
  4. 添加了您的模型文件 - 文章和房间
  5. 在db / seeds.rb中添加了测试数据以填充数据库(见下文)
  6. 然后我做了一些改变:

    1. 为学校添加了一个脚手架:rails g scaffold School title:string(必须删除添加到db / migrate的迁移文件)
    2. 更新了学校模型(见下文)
    3. 更新了学校视图(show.html.erb,见下文)
    4. 然后我测试了它:

      1. 已解雇rails s
      2. 导航至http://localhost:3000/schools
      3. 点击“显示”为“你想要展示的人”
      4. 瞧!
      5. Screenshot of final view

        分贝/ seeds.rb

        School.delete_all
        
        School.create(id: 1, title: "The One You Want To Show")
        School.create(id: 2, title: "Some Other School")
        
        Article.delete_all
        
        Article.create(id: 2, school_id: 1, day: 1, start_time: "09:00", title: "Math")
        Article.create(id: 4, school_id: 1, day: 2, start_time: "10:00", title: "English")
        Article.create(id: 5, school_id: 1, day: 1, start_time: "11:00", title: "Science")
        Article.create(id: 8, school_id: 2, day: 3, start_time: "12:00", title: "Physics")
        Article.create(id: 9, school_id: 1, day: 2, start_time: "13:00", title: "Music")
        
        Room.delete_all
        
        Room.create(id: 3, school_id: 1, day: 1, name: "classA")
        Room.create(id: 6, school_id: 1, day: 2, name: "classB")
        Room.create(id: 7, school_id: 2, day: 3, name: "classC")
        

        应用程序/模型/ school.rb

        class School < ActiveRecord::Base
          has_many :rooms
          has_many :articles
        
          def days
            self.articles.pluck(:day).uniq
          end
        end
        

        应用程序/视图/学校/ show.html.erb

        <p id="notice"><%= notice %></p>
        
        <p> 
          <strong>Title:</strong>
          <%= @school.title %>
        </p>
        
        <% @school.days.each do |d| %>
        
          <h3>Day<%= d %></h3>
        
          <% @school.articles.where(day: d).each do |a| %>
            <%= a.start_time %>
            <%= a.title %><br>
          <% end %>
        
          <p>Room <%= @school.rooms.where(day: d).first.name %></p>  
        
        <% end %>
        
        </br>
        <%= link_to 'Edit', edit_school_path(@school) %> |
        <%= link_to 'Back', schools_path %>
        

        如果我是你,我可能会将#where.(day: d)...从视图重构为模型,但这很小。

        作为事后的想法,您可以将文章和会议室模型从您的问题中简化为:

        class Article < ActiveRecord::Base
          belongs_to :school
        end
        
        class Room < ActiveRecord::Base
          belongs_to :school
        end
        

答案 1 :(得分:1)

我会尽力帮助:

<强>控制器

在本节中,您需要选择仅需要的字段。然后,将文章表与房间连接。

@articles = Article.select("articles.id, articles.title, articles.contents, articles.category, articles.created_at, rooms.name AS room_name").joins(:rooms).group_by{|x| [x.created_at, x.room_name]}

<强>模型

删除matching_detail方法,因为您不需要此方法。它的方法会在循环中加载数据,所以我将很长时间加载你的数据。

查看

对于视图,您必须编辑一些代码,以便在下面显示。

<div class="row">
  <% @articles.each do |data, articles| %>
    <h3>Day <%= data[0] %></h3>
    <% articles.each do |a| %>
      <%= a.start_time %>
      <% a.category %>
      <%= a.contents %><br>
    <% end %>
    <%= data[1] %><br> #I'd like to display only one time
  <% end %>
</div>

备注: data[0]代表一天,data[1]代表房间名称 我希望这能帮到你。