【问题标题】:Rails 3.1 build associationRails 3.1 建立关联
【发布时间】:2011-07-14 22:18:38
【问题描述】:

所以我有一个流程模型和一个页面模型

每个流程都有_许多页面,每个页面都属于_一个流程

流动模型

class Flow < ActiveRecord::Base
has_many :pages, :dependent => :destroy
accepts_nested_attributes_for :pages, :reject_if => lambda { |p| p[:path].blank?}, :allow_destroy => true
end

页面模型

class Page < ActiveRecord::Base
  belongs_to :flow
end

然后在新的流程动作中我有这个

def new
    @flow = Flow.new
    3.times do
      page = @flow.pages.build
    end
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @flow }
    end
  end

但我不断收到错误消息:“未知属性:flow_id”?

【问题讨论】:

  • 您的Page 模型确实有一个flow_id 字段,对吗? (并且您已经运行了迁移以确保数据库拥有它?)
  • 为流和页表显示文件 db/schema.rb

标签: ruby-on-rails controller associations ruby-on-rails-3.1


【解决方案1】:

使用generate migration add_flow_id_to_page flow_id:integer 生成外键列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多