0

I have 2 models UnitPlate & PlantPlate which are a part of STI(Single table inheritance). I need many-to-many association with them. Is the below way of has_many: through and STI correct. I have the below models. Facing some issues in migrations with the below approach

Parent Model

class Plate < ActiveRecord::Base

end

Submodel 1

class UnitPlate < Plate

  has_many :unit_and_plant_plates, :dependent => :destroy
  has_many :plant_plates, through: :unit_and_plant_plates

end

Submodel 2

class PlantPlate < Plate

  has_many :unit_and_plant_plates, :dependent => :destroy
  has_many :unit_plates, through: :unit_and_plant_plates

end

Associated Model

class UnitAndPlantPlate < ActiveRecord::Base
    belongs_to :unit_plate
    belongs_to :plant_plate
end

Migration

class CreateTableUnitAndPlantPlates < ActiveRecord::Migration
  def change
     create_table :unit_and_plant_plates do |t|
      t.belongs_to :unit_plate
      t.belongs_to :plant_plate
      t.timestamps
     end
    add_index :unit_and_plant_plates, :unit_plate_id
    add_index :unit_and_plant_plates, :plant_plate_id
   end
end

Error during migrations

rake aborted!
/Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/trace_output.rb:16:in `block in trace_on': invalid byte sequence in US-ASCII (ArgumentError)
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/trace_output.rb:14:in `map'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/trace_output.rb:14:in `trace_on'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:340:in `trace'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:187:in `display_error_message'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:174:in `rescue in standard_exception_handling'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/bin/rake:19:in `load'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/bin/rake:19:in `<main>'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
    from /Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'

Is this way of defining the has_many through correct?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
AnkitG
  • 6,438
  • 7
  • 44
  • 72
  • What issues are you experiencing with migrations? – Peter Brown Jul 31 '13 at 12:24
  • @Beerlington, updated the ques with error – AnkitG Jul 31 '13 at 12:29
  • I'd put a debugger in `/Users/ankitgupta/.rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/trace_output.rb:16`, search for the variable holding the invalid US-ASCII sequence, and follow it up the stack. – tessi Jul 31 '13 at 12:43
  • guys the problem i see is with the implementation. The other migrations work. It will be great if you can review the approach that i have used. I am not confident if it's right – AnkitG Jul 31 '13 at 12:46
  • @AnkitG Did you ever solve this? I just ran into the exact same problem – rmcsharry Sep 16 '16 at 04:17

1 Answers1

0

Try this:

class CreateTableUnitAndPlantPlates < ActiveRecord::Migration
  def change
     create_table :unit_and_plant_plates do |t|
      t.integer :unit_plate_id
      t.integer :plant_plate_id
      t.timestamps
     end
    add_index :unit_and_plant_plates, :unit_plate_id
    add_index :unit_and_plant_plates, :plant_plate_id
   end
end
Aman Garg
  • 4,198
  • 2
  • 21
  • 29
  • I tried this, but this won't work as this is an STI pattern, there is nothing like a `unit_plates` or `plant_plates`. – AnkitG Jul 31 '13 at 12:49