Rails中的一对多关系 - MissingAttribute错误

时间:2014-06-08 17:59:37

标签: ruby-on-rails ruby activerecord

我不明白为什么s.pages << page1在Rails 4.1.1中没有用(我在rails控制台中)。 我的问题的更多背景:我有一个Subject.rb和Page.rb作为模型,定义如下:

class Subject < ActiveRecord::Base
    has_many :pages
    scope :visible, lambda { where (:visible => true) }
end

class Page < ActiveRecord::Base
    belongs to :subject 
end

在DB \ Migrate中,我有2个已执行的迁移文件:

class CreateSubjects < ActiveRecord::Migration
  def up
    create_table :subjects do |t|
        t.string "name"
        t.integer "position"
        t.boolean "visible", :default => false
        t.timestamps
    end
  end

  def down
    drop_table :subjects
  end


end

对于Pages:

class CreatePages < ActiveRecord::Migration
    def up
        create_table :pages do |t|
            t.integer "subject_id"
            t.string "name"
            t.string "permalink"
            t.integer "position"
            t.boolean "visible", :default => false
            t.timestamps
        end
        add_index("pages", "subject_id")
        add_index("pages", "permalink")

    end

    def down
        drop_table :pages
    end

end

在rails控制台中,我键入s = Subject.find_by_id(1)并获得预期结果,来自主题表的记录,其id = 1 然后,我输入:page1= Page.new(:name => "McCaine", :permalink => "first", :position => 1) 并且创建了一条记录但尚未插入数据库中(因此此时它的页面ID = nil),因为它需要首先与它所属的主题相关联。 因此,我输入s.pages << page1 但是我得到了这个MissingAttribute错误:

  

←[1m←[35m(1.0ms)←[0m BEGIN←[1m←[36mSQL(4.0ms)←[0m←[1mINSERT   INTO&#34;页面&#34; (&#34; created_at&#34;,&#34; name&#34;,&#34; permalink&#34;,&#34; position&#34;,   &#34; subject_id&#34;,&#34; updated_at&#34;)价值($ 1,$ 2,$ 3,$ 4,$ 5,$ 6)返回   &#34; id&#34;←[0m [[&#34; create d_at&#34;,&#34; 2014-06-08 17:48:22.843764&#34;],[&#34; name&# 34 ;,   &#34; McCaine&#34;],[&#34;永久链接&#34;,&#34;第一&#34;],[&#34;位置&#34;,1],[&#34; subject_id&# 34 ;,   1],[&#34; updated_at&#34;,&#34; 2014-06-08 17:48:22.843764&#34;]]←[1m←[35m   (1.0ms)←[0m ROLLBACK ActiveModel :: MissingAttributeError:不能写   未知属性page_id' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.1.1/lib/active_record/attribute_methods/write.rb:72:in write_attribute&#39;           来自C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.1.1/lib/active_record/attribute_methods/dirty.rb:68:in   write_attribute' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-4.1.1/lib/active_record/attribute_methods.rb:390:in [] =&#39;          ....

任何帮助?

提前致谢。

稍后修改:如rake db:migrate:status所示,Page模型已启动:

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20140531184836  Create users
   up     20140601105035  Alter users
   up     20140601212417  Create subjects
   up     20140601212440  Create pages
   up     20140601212504  Create sections

1 个答案:

答案 0 :(得分:0)

我也在关注Lynda教程。

您似乎错过了下划线。

class Subject < ActiveRecord::Base
    has_many :pages
    scope :visible, lambda { where (:visible => true) }
end

class Page < ActiveRecord::Base
    #belongs to :subject 
    belongs_to :subject 
end

我遇到了同样的错误,但是有一个小问题。我在Page和Subject中都使用了belongs_to。