0

私は何日もこの問題の修正に取り組んできました。エラーが発生し続けます:

ActiveRecord::AssociationTypeMismatch in SatellitesController#create

Satellite(#70098878574220) expected, got String(#70098849353340)

このサイト全体を見てきましたが、何も役に立たないようです。これは、これまでのコードにあるものです。

私の new.html.erb ファイルでは:

<%= form_for( @satellite ) do |f| %>
<div class="field">
    <%= f.label :parent_id %></br>
    <%= f.select( :parent_id, Satellite.all.collect { |s| [ s.name, s.id ] }, { :include_blank => '-select-' } ) %>
</div>

そして、これは私が使用している関連付けです:

class Satellite < ActiveRecord::Base

  validates :name, :presence => true, :uniqueness => true

  has_many :satellites, class_name: 'Satellite', foreign_key: 'parent_id'
  belongs_to :parent_id, class_name: 'Satellite', foreign_key: 'parent_id'
end

どんな助けでも大歓迎です!

4

1 に答える 1

0

collection_selectではなくselectを使用する理由に関連しているかどうかわかりませんか?

<%= f.select( :parent_id, Satellite.all.collect { |s| [ s.name, s.id ] }, { :include_blank => '-select-' } ) %>

になり得る

<%= f.collection_select :parent_id, Satellite.all, :id, :name, { :include_blank => true } %>

また、モデルの自己参照セクションでの親への参照が間違っている可能性があります。

belongs_to :parent_id, class_name: 'Satellite', foreign_key: 'parent_id'

これであるべきだと思います

belongs_to :satellites, class_name: 'Satellite', foreign_key: 'parent_id'
于 2013-03-17T07:17:02.270 に答える