0
我有2個模型:Doc和Row。 Doc有許多行和一行belongs_to單個文檔。Rails 4 - 兩個模型設置爲一對多關聯,但不能拉出與一個模型相關的所有記錄
這裏是模型看起來像在代碼:
Doc:
# == Schema Information
#
# Table name: docs
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# numrows :integer
# user :string(255)
# source :string(255)
# size :string(255)
# location :string(255)
# status :string(255)
#
class Doc < ActiveRecord::Base
has_many :rows
行:
# == Schema Information
#
# Table name: rows
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# numCols :integer
# type :string(255)
# content :text
# status :string(255)
# doc_id :integer
#
class Row < ActiveRecord::Base
belongs_to :doc, :foreign_key => "doc_id", :class_name => "Doc"
=begin
Status of a row can be:
1) header
2) body
3) ignore
4) nil
=end
現在在軌控制檯下面是命令集進出口運行:
2.0.0-p247 :084 > @doc = Doc.find(9)
Doc Load (0.7ms) SELECT `docs`.* FROM `docs` WHERE `docs`.`id` = 9 LIMIT 1
=> #<Doc id: 9, created_at: "2014-07-14 16:42:54", updated_at: "2014-07-14 16:42:59", numrows: 761, user: "XXXX", source: nil, size: nil, location: "ABC.csv", status: "parsing_complete">
2.0.0-p247 :085 > @doc.rows
NoMethodError: undefined method `rows' for #<Doc:0x007ff20c1b8860>
from /Users/XXXX/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/XXXX/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from (irb):85
from /Users/XXXX/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/xxxx/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/XXXX/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p247 :086 >
2.0.0-p247 :086 > Row.where(:doc_id => 9)
Row Load (31.7ms) SELECT `rows`.* FROM `rows` WHERE `rows`.`doc_id` = 9
=> #<ActiveRecord::Relation [#<Row id: 1, created_at: "2014-07-14 16:42:57", updated_at: "2014-07-14 16:42:57", numCols: nil, type: nil, content: "[PROGRAMS] (All Locations|Consolidated),,,,,,,,,,,,...">,
,
....
因此Row對象存在。但是,我不能通過相應的Doc模型對象拉它們。
因爲我建立了所有的關聯。我想知道我沒有得到什麼招數
任何幫助,將不勝感激。
感謝