0
我正將應用程序從rails 5.0遷移到5.1,並且在訪問相關模型時出現錯誤'Cant cast Hash'(一切正常5.0)。Rails 5.0 to 5.1 - 'Cant cast Hash'on one-to-many association
sighting.rb:
belongs_to :species, :class_name => 'Species', :foreign_key => 'species_id'
belongs_to :site, :class_name => 'Site', :foreign_key => 'site_id'
belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
belongs_to :spotter, :class_name => 'User', :foreign_key => 'spotter_id'
species.rb:
has_many :sightings, :class_name => 'Sighting', :foreign_key => 'species_id', :dependent => :restrict_with_exception
site.rb:
has_many :sightings, :class_name => 'Sighting', :foreign_key => 'site_id', :dependent => :restrict_with_exception
在控制檯(已接種的數據庫):
Sighting.find(1).site
返回正確的網站模型。
Sighting.find(1).species
在另一方面返回錯誤
TypeError: can't cast Hash
兩者相關聯的模型(以及在用戶模型 - 未示出)都設置了以相同的方式。正如我所說,如果我恢復到軌5.0再一次正常工作。