【问题标题】:linking to table and returning value链接到表并返回值
【发布时间】:2012-07-28 05:41:04
【问题描述】:

我正在尝试查询一个表并在满足条件时返回一个值。

调度模型

class Schedule < ActiveRecord::Base
  belongs_to :result
  attr_accessible :result_id
end

结果模型

class Result < ActiveRecord::Base
  has_one :schedule
  attr_accessible :against, :for, :schedule_id
end

schedules.haml

- if schedule.result
  Result
- else
  No Result

我得到的只是没有结果,即使我知道已经为调度 ID 加载了结果

任何帮助都会非常感谢

【问题讨论】:

  • 您的 schedules.haml 中的上述示例格式是否正确? HAML 依赖于缩进。 “- else”应与“- if”对齐。
  • 是的,它的格式正确,我得到的只是每个人都没有结果,这是不正确的

标签: ruby-on-rails activerecord belongs-to has-one


【解决方案1】:

看看这个网站:

http://rubyquicktips.com/post/3096503536/how-to-check-if-objects-or-relations-exist

你可能想尝试类似的东西

schedule.result.any?

另外,试试:

raise schedule.result.to_yaml

查看此语句返回的确切内容

【讨论】:

  • 使用= schedule.results.present? 并在我期望的地方得到了真实的结果,但现在我查询时怎么会,即 schedule.results.for 它没有返回结果?
  • schedule.result(不是复数)应该返回一个计划对象。但是,我不熟悉 .for 方法
  • for 是我的日程模型中的一个对象
  • 这样做我得到了undefined method result' #<0xa4333fc>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2015-10-01
  • 1970-01-01
相关资源
最近更新 更多