2011-06-17 53 views
2

錯誤使用關係

uninitialized constant Suspicious::Activity::SuspiciousPerson 

模型結構

app 
    models 
    suspicious 
     activity.rb 
     person.rb 

模型類

class Suspicious::Activity < ActiveRecord::Base 
    has_many :suspicious_people, :dependent => :destroy 
    accepts_nested_attributes_for :suspicious_people, :allow_destroy => true 

class Suspicious::Person < ActiveRecord::Base 
    belongs_to :suspicious_activity 

這就是發生錯誤[3號線]

1 def new 
2 @activity = Suspicious::Activity.new 
3 @activity.suspicious_people.build 
4 end 

回答

1

當你命名空間模型這樣,你不需要引用到車型具有相同的名稱時,前面加上「suspicious_」。

模型

class Suspicious::Activity < ActiveRecord::Base 
    has_many :people, :dependent => :destroy 
    accepts_nested_attributes_for :people, :allow_destroy => true 
end 

class Suspicious::Person < ActiveRecord::Base 
    belongs_to :activity 
end 

控制器

def new 
    @activity = Suspicious::Activity.new 
    @activity.people.build 
end