尝试从其他表中引用关联值时遇到上述错误。
我有两个具有一对多关联的模型。
class Roast < ApplicationRecord
has_one :processing
class Processing < ApplicationRecord
belongs_to :roast
我已将process_id
列添加到Roasts
表中。
Roasts
代表单独的咖啡烘焙,processing
代表该咖啡生产中的三个过程之一。
Processing
填充了三个静态字符串,我可以通过“ collection_select”字段以烤肉形式使用它们。
<%= form.collection_select(:process_id, Processing.order(:coffeeprocess), :id, :coffeeprocess, :prompt => 'Select Process') %>
我能够为单个烤肉分配一个过程,并且我可以在process_id
列中看到相应的ID。
但是,当我进入roasts
显示页面时,出现错误:undefined method 'coffeeprocess' for nil:NilClass
,据我所知,该值是Nil。
我在演出页面中使用了以下内容。
<%= @roast.processing.coffeeprocess %>
有什么想法要解决吗?
答案 0 :(得分:0)
您之所以得到nil:NilClass
,是因为Rails不了解Roasts和Processing之间的关系。
尝试
class Roast < ApplicationRecord
belongs_to :processing
有关更多信息,请参见documentation for ActiveRecord Associations。