ruby-on-rails - Rails4 : How to display the value one time only

标签 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

我想在 View 中显示如下数据;

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

最佳答案

我通过以下方式重新创建了您的 Rails 项目:

  1. rails new SchoolScheduler
  2. 从你的问题中添加了模式文件
  3. rails db:schema:load
  4. 添加了您的模型文件 - 文章和房间
  5. 在 db/seeds.rb 中添加您的测试数据以填充数据库(见下文)

然后我做了一些修改:

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

然后我测试了一下:

  1. 启动 rails s
  2. 导航到 http://localhost:3000/schools
  3. 点击“显示”以显示“您要显示的那个”
  4. 瞧!

Screenshot of final view

db/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

app/views/school/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 %>

如果我是你,我可能会将 View 中的 #where.(day: d)... 重构到模型中,但这是次要的。

事后,您可以将问题中的 Article 和 Room 模型简化为:

class Article < ActiveRecord::Base
  belongs_to :school
end

class Room < ActiveRecord::Base
  belongs_to :school
end

关于ruby-on-rails - Rails4 : How to display the value one time only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726789/

相关文章:

ruby-on-rails - 有没有办法让:reserved_words option for models in FriendlyId gem does not overwrite the Rails defaults?

mysql - 我如何使用 mysql 创建 Rails 3.1 应用程序

mysql - 如何通过 IRB 中的 Mysql2 Gem 中的一次调用运行多个 sql 查询?

ruby-on-rails - 如何在我的 Rails 应用程序中获取所有路由?

ios - 设计:iOS “Add to Home Screen”

ruby-on-rails - ruby 和 ruby​​ on rails 版本的命名依据是什么

ruby-on-rails - 为什么这个作用域的 lambda 后面跟着一个 block ?

ruby - 在 ruby​​ 代码中调用 AWS lambda 函数

mysql - 按另一个相关模型属性排序 - Ruby on Rails

ruby-on-rails - Rails 4 belong/has_many 关系 - 删除关联属性但保留关联记录