Ruby on rails ROR-索引上无类,但未显示

Ruby on rails ROR-索引上无类,但未显示,ruby-on-rails,class,null,associations,Ruby On Rails,Class,Null,Associations,我一直在试图找到这个问题的答案,但运气不好。我想这是一个关联问题,可能是一个新手的错误(我就是其中之一)。 这就是功能: 我需要为特定的个人资料制作一包啤酒(我知道啤酒的一切听起来都很有趣,但它让我很难受) 我有3种型号: 啤酒模型: class Beer < ActiveRecord::Base include PermissionsConcern validates :name, presence: true has_many :ratings has_many :u

我一直在试图找到这个问题的答案,但运气不好。我想这是一个关联问题,可能是一个新手的错误(我就是其中之一)。 这就是功能: 我需要为特定的个人资料制作一包啤酒(我知道啤酒的一切听起来都很有趣,但它让我很难受)

我有3种型号:

啤酒模型:

class Beer < ActiveRecord::Base
  include PermissionsConcern
  validates :name, presence: true

  has_many :ratings
  has_many :users, through: :ratings
  has_many :packs
end
在show视图中,我执行以下操作:

@pack.beer.name #works fine.
@pack.profile.name #WORKS FINE ALSO
我尝试在控制台中执行此操作,结果相同:

Pack.last.profile.name # works fine

Pack.all # works and shows the profile_id correctly.

packs = Pack.all
packs.each do |p|
print p.beer.name #works fine
print p.profile.name #nil class again
end
以防万一,我包括了模式:

create_table "beers", force: :cascade do |t|
  t.string   "name",               limit: 255
  t.datetime "created_at",                       null: false
  t.datetime "updated_at",                       null: false
  t.string   "beer_type",          limit: 255
end

create_table "packs", force: :cascade do |t|
  t.date     "delivery_date"
  t.integer  "profile_id",    limit: 4
  t.integer  "beer_id",       limit: 4
  t.integer  "status",        limit: 4
  t.datetime "created_at",              null: false
  t.datetime "updated_at",              null: false
end

add_index "packs", ["beer_id"], name: "index_packs_on_beer_id", using: :btree
add_index "packs", ["profile_id"], name: "index_packs_on_profile_id", using: :btree

create_table "profiles", force: :cascade do |t|
  t.string   "ibu_range",  limit: 255
  t.datetime "created_at",             null: false
  t.datetime "updated_at",             null: false
  t.string   "name",       limit: 255
end


add_foreign_key "packs", "beers"
add_foreign_key "packs", "profiles"
end

我试图尽可能详细地解释情况。有人能帮我理解我做错了什么吗?谢谢

某些包可能没有配置文件

由于您正在使用console,请尝试以下操作:

Pack.all.select{|item| item.profile.nil?}.size

如果大小>0,而您不希望这样,请添加
验证:配置文件,状态:true

您是否也可以添加更新操作,以便我们可以查看您如何保存对象?谢谢!!我有一个项目。简介。零?是的,这把事情搞砸了。这正是问题所在。谢谢!!
@pack.beer.name #works fine.
@pack.profile.name #WORKS FINE ALSO
Pack.last.profile.name # works fine

Pack.all # works and shows the profile_id correctly.

packs = Pack.all
packs.each do |p|
print p.beer.name #works fine
print p.profile.name #nil class again
end
create_table "beers", force: :cascade do |t|
  t.string   "name",               limit: 255
  t.datetime "created_at",                       null: false
  t.datetime "updated_at",                       null: false
  t.string   "beer_type",          limit: 255
end

create_table "packs", force: :cascade do |t|
  t.date     "delivery_date"
  t.integer  "profile_id",    limit: 4
  t.integer  "beer_id",       limit: 4
  t.integer  "status",        limit: 4
  t.datetime "created_at",              null: false
  t.datetime "updated_at",              null: false
end

add_index "packs", ["beer_id"], name: "index_packs_on_beer_id", using: :btree
add_index "packs", ["profile_id"], name: "index_packs_on_profile_id", using: :btree

create_table "profiles", force: :cascade do |t|
  t.string   "ibu_range",  limit: 255
  t.datetime "created_at",             null: false
  t.datetime "updated_at",             null: false
  t.string   "name",       limit: 255
end


add_foreign_key "packs", "beers"
add_foreign_key "packs", "profiles"
end
Pack.all.select{|item| item.profile.nil?}.size