Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 Rails4:关联数据不存在时如何显示数据';不存在_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails4:关联数据不存在时如何显示数据';不存在

Ruby on rails Rails4:关联数据不存在时如何显示数据';不存在,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我想做的是如下显示数据: 如果a.title存在,但@school.rooms.where(day:d).first.name不存在,则显示a.title 如果a.title不存在,但@school.rooms.where(day:d).first.name存在,则显示@school.rooms.where(day:d).first.name 如果两个数据都存在,它就可以工作。 虽然我尝试了一些代码,比如exist?,nil?等等,但我还是做不到 如果您能给我提供Rails的最佳方式,我将不

我想做的是如下显示数据:

  • 如果
    a.title
    存在,但
    @school.rooms.where(day:d).first.name
    不存在,则显示
    a.title
  • 如果
    a.title
    不存在,但
    @school.rooms.where(day:d).first.name存在,则显示
    @school.rooms.where(day:d).first.name
如果两个数据都存在,它就可以工作。 虽然我尝试了一些代码,比如
exist?
nil?
等等,但我还是做不到

如果您能给我提供Rails的最佳方式,我将不胜感激

视图代码

<div class="row">
  <h1 class="col-md-12"><%= @school.title %></h1>
</div>
<div class="row">

  <% @school.days.each do |d| %>

    <h3>Day<%= d %></h3>

    <% @school.articles.where(day: d).each do |a| %>
      <strong><%= a.title %></strong></p>
    <% end %>

    <p>Room: <%= @school.rooms.where(day: d).first.name %></p>

  <% end %>

</div>

如果您能给我任何建议,我们将不胜感激。

如果您总是有一段数据(即
a.title
存在,如果
@school.rooms.where(day:d).first.name
不存在),您可以使用以下内容:

<p>Room: <%= a.title || @school.rooms.where(day: d).first.name %></p>
房间:

如果两者都不存在,您应该能够使用“默认”值:

房间:


我喜欢第二个答案。谢谢你的回答,@Rich Peck。是否可以在每次结束后使用
a.title
?虽然我尝试过,但还是显示了NameError“undefined local variable or method“a”。不,最后一个条目是“default”——其他所有条目都失败了。谢谢您的回答,@Rich Peck。虽然我尝试了最后一个条目
房间:

,但显示了以下错误消息<代码>未定义的局部变量或#的方法“a”
class School < ActiveRecord::Base
    has_many :articles
    has_many :rooms

    def days
      self.articles.pluck(:day).uniq
    end
end


class Article < ActiveRecord::Base
    belongs_to :school
end


class Room < ActiveRecord::Base
    belongs_to :school
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
<p>Room: <%= a.title || @school.rooms.where(day: d).first.name %></p>
<p>Room: <%= a.title || @school.rooms.where(day: d).first.name || "default" %></p>