ActiveRecord :: StatementInvalid at / category / 5

时间:2015-10-01 22:43:07

标签: ruby postgresql activerecord rails-activerecord

我是一名新的开发人员,负责Sinatra / Ruby / ActiveRecord应用程序。我已经在Category和Recipe之间建立了1:many(postgres)关系。当我尝试列出类别中的食谱时,我收到错误:

ActiveRecord :: StatementInvalid at / category / 5 PG :: UndefinedColumn:错误:列recipes.category_id不存在第1行:选择1作为一个FROM"食谱" WHERE"食谱"。" category_id ... ^:SELECT 1 AS FROM FROM"食谱"在哪里"食谱"。" category_id" = $ 1 LIMIT 1 file:postgresql_adapter.rb location:prepare line:637

app.rb

get('/category/:id') do
  @category = Category.find(params.fetch('id'))
  @recipes = @category.recipes
  erb(:category)
end

category.erb

<% if @recipes.any?() %>
  <% @recipes.each() do |recipe| %>
    <ul>
      <li><a href='/recipe/<%=recipe.id%>'> <%=recipe.recipe_name%></a></li>
    </ul>
  <%end%>
<%else %>
  <p>You have no recipes!</p>
<%end%>

模式

ActiveRecord::Schema.define(version: 20150930234556) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "categories", force: :cascade do |t|
    t.string   "cat_name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "images", force: :cascade do |t|
    t.string   "image_name"
    t.string   "url"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "alt"
    t.integer  "width"
    t.integer  "height"
    t.integer  "recipe_id"
  end

  create_table "ingredients", force: :cascade do |t|
    t.string   "ingredient_name"
    t.integer  "recipe_id"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
  end

  create_table "instructions", force: :cascade do |t|
    t.string   "instruction_name"
    t.integer  "recipe_id"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end

  create_table "recipes", force: :cascade do |t|
    t.string   "recipe_name"
    t.string   "source"
    t.string   "servings"
    t.string   "comment"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "cat_id"
  end
end

我搜索了我的项目文件夹,找不到category_id。不知道为什么要找它,我的字段名是category.id和recipe.cat_id。

1 个答案:

答案 0 :(得分:2)

此:

@recipes = @category.recipes

暗示你有

has_many :recipes

Category模型中。 has_many category_id会在recipes表中找到category_id列(即Recipe模型中的recipes.category_id属性。但是您没有recipes.cat_id列,而是有create_table "recipes", force: :cascade do |t| #... t.integer "cat_id" end 列:

recipes.cat_id

我建议将recipes.category_id列重命名为:foreign_key,以匹配Rails非常强烈偏好的约定。如果您无法重命名该列,请向has_many添加Category选项,告诉has_many :recipes, :foreign_key => :cat_id 如何解决该关系:

FILE_PATTERNS = *.html *.htm \
                *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ \
                *.inl *.idl *.ddl *.odl *.h *.hh *.hxx *.hpp *.h++ *.cs \
                *.d *.php *.php4 *.php5 *.phtml *.inc *.m *.markdown *.md \
                *.mm *.dox *.py *.f90 *.f *.for *.tcl *.vhd *.vhdl *.ucf \
                *.qsf *.as *.js