【问题标题】:Single Table Inheritance with Formtastic使用 Formtastic 的单表继承
【发布时间】:2011-07-14 11:59:48
【问题描述】:

所以我有两个模型继承自另一个模型,如下所示:

class OneTime < Step
  has_one :due_date
  accepts_nested_attributes_for :due_date
end

class Repeatable < Step
  has_many :due_dates
  accepts_nested_attributes_for :due_dates
end

class Step < ActiveRecord::Base
  belongs_to :goal
end

我还有另一个嵌套在子类中的模型:

class DueDate < ActiveRecord::Base
  belongs_to :step
end

然后我有一个像这样使用formtastic的表格:

= semantic_form_for [@goal, @step], :url => goal_step_path(@goal, @step), :html => { :class => "ajax"} do |f|
  = f.inputs :id => "step_#{@step.id}_description", :class => "description" do
    = f.input :description, :input_html => { :autofocus => "autofocus"}
  = f.semantic_fields_for :due_date do |due_date|
    = due_date.inputs :date_due, :as => :string, :input_html => { :class => "datepicker"}, :label => "Due Date"
  = f.buttons do
    = f.commit_button

问题是当我尝试加载该表单时出现以下错误:

Showing /Users/dc/launchtools/app/views/steps/_form.html.haml where line #4 raised:

SQLite3::SQLException: no such column: due_dates.one_time_id: SELECT  "due_dates".* FROM "due_dates" WHERE ("due_dates".one_time_id = 121) LIMIT 1
Extracted source (around line #4):

我尝试了很多不同的方法来修复它,但没有任何效果。我还搜索了 stackoverflow 和 google 并没有找到任何有助于解决这个问题的东西。也许我没有正确解决问题,但我需要一些方向。有什么想法吗?

【问题讨论】:

    标签: ruby ruby-on-rails-3 formtastic single-table-inheritance


    【解决方案1】:

    您尝试将多个参数传递给它认为是多个字段的due_date.inputs,您想改用due_date.input(单数),如下所示:

    = due_date.input :date_due, :as => :string, :input_html => { :class => "datepicker"}, :label => "Due Date"
    

    【讨论】:

    • 自从发布以来,我已经离开了 STI,因为没有它我的问题似乎得到了更好的解决,但你对单数与复数的看法是对的。我没有注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2015-12-04
    • 2010-09-17
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多