inheritance - Rails: ActiveRecord::AssociationTypeMismatch: Superclass expected, got Subclass -
i trying create class model application has next key features:
inheritance (the classplace
acts superclass farm
, depot
). added gem multiple_table_inheritance implement this. relationship place
(can have many places
) i using rails 3.2.11 , ruby 1.9.3.p194.
here rough class model of trying implement.
you can find relationship definition in place
model:
class place < activerecord::base acts_as_superclass attr_accessible :location, :name, :subtype has_many :place_connections, foreign_key: :place_a_id, dependent: :destroy has_many :places, through: :place_connections, source: :place_b has_many :reverse_place_connections, class_name: :placeconnection, \ foreign_key: :place_b_id, dependent: :destroy end
the farm
model can seen in following. depot
model looks same.
class farm < activerecord::base inherits_from :place end
however, when check relationship model in rails console experience following:
> farm = place.first => #<farm place_id: 1, created_at: "2013-02-08 12:19:16", \ updated_at: "2013-02-08 12:19:16"> > depot = place.last => #<depot place_id: 6, created_at: "2013-02-08 12:19:44", \ updated_at: "2013-02-08 12:19:44"> > farm.places = [depot] activerecord::associationtypemismatch: place(#42600420) expected, \ got depot(#42518820) ...
can tell whether configured relationship correctly? doing wrong? maybe mixed singular , plural names models , associations.
problem identifiedi think (a friend of mine , me) found out problem is: whenever database contains relation of 2 places
, trying replace pair of ids exact same pair type mismatch error raised. error not pop if replace existing pair another pair.
before:
placeconnection ----------------- place_a | place_b ----------------- 1 6 -----------------
action:
> farm = place.first => #<farm place_id: 1, created_at: "2013-02-08 12:19:16", \ updated_at: "2013-02-08 12:19:16"> > depot = place.last => #<depot place_id: 6, created_at: "2013-02-08 12:19:44", \ updated_at: "2013-02-08 12:19:44"> > farm.places = [depot.place] activerecord::associationtypemismatch: place(#42600420) expected, \ got depot(#42518820) ...
working example: before:
placeconnection ----------------- place_a | place_b ----------------- 3 2 -----------------
action:
> farm = place.first => #<farm place_id: 1, created_at: "2013-02-08 12:19:16", \ updated_at: "2013-02-08 12:19:16"> > depot = place.last => #<depot place_id: 6, created_at: "2013-02-08 12:19:44", \ updated_at: "2013-02-08 12:19:44"> > farm.places = [depot.place] => [#<place id: 6, name: "some depot", location: "somewhere", subtype: "depot", \ created_at: "2013-02-09 12:51:01", updated_at: "2013-02-09 12:51:01">]
please note another thing realized is , time possible extend array of relations follows: farm.places << depot.place
. want utilize anyways. nevertheless, assignment problem might bug?!
on lastly line, seek replacing this:
farm.places = [depot]
with this:
farm.places = [depot.place]
the .place
method should homecoming superclass instance of place
, (which technically placeconnection
referencing) instead of subclass instance of depot
.
ruby-on-rails inheritance has-many type-mismatch
No comments:
Post a Comment