0

I've read a lot of SO questions on this and migration guides and api and am not clear. Most of the answers assume you have models and tables for the two entities. I'm starting from scratch so need a full solution. I have drugs and drug classes. Drugs can be in many drug classes and drug classes have many drugs. What's the best way to set this up? I'd like to use rails generate.

using: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin11.4.0] Rails 4.0.0.beta1

Where can I use generate and where do I generate something to start and then edit the files created by the generate command?

I don't get the difference between has and belongs to many and has many through.

I've run so far....

Switched to branch 'DrugClasses'
➜  lm git:(DrugClasses) ✗ rails generate model Drug name:string
  invoke  active_record
  create    db/migrate/20130502191118_create_drugs.rb
  create    app/models/drug.rb
  invoke    rspec
  create      spec/models/drug_spec.rb
  invoke      factory_girl
  create        spec/factories/drugs.rb
➜  lm git:(DrugClasses) ✗ rails generate model Drug_Class name:string
  invoke  active_record
  create    db/migrate/20130502191208_create_drug_classes.rb
  create    app/models/drug_class.rb
  invoke    rspec
  create      spec/models/drug_class_spec.rb
  invoke      factory_girl
  create        spec/factories/drug_classes.rb
➜  lm git:(DrugClasses) ✗ rails generate model Drugs_Drug_Classes drug:references drug_class:references
  invoke  active_record
  create    db/migrate/20130502191606_create_drugs_drug_classes.rb
  create    app/models/drugs_drug_classes.rb
  invoke    rspec
  create      spec/models/drugs_drug_classes_spec.rb
  invoke      factory_girl
  create        spec/factories/drugs_drug_classes.rb
➜  lm git:(DrugClasses) ✗ 

and here is what's been generated.

➜  lm git:(DrugClasses) ✗ cat db/migrate/20130502191118_create_drugs.rb 
class CreateDrugs < ActiveRecord::Migration
  def change
    create_table :drugs do |t|
      t.string :name

      t.timestamps
    end
  end
end
➜  lm git:(DrugClasses) ✗ cat db/migrate/20130502191208_create_drug_classes.rb 
class CreateDrugClasses < ActiveRecord::Migration
  def change
    create_table :drug_classes do |t|
      t.string :name

      t.timestamps
    end
  end
end
➜  lm git:(DrugClasses) ✗ cat db/migrate/20130502191606_create_drugs_drug_classes.rb 
class CreateDrugsDrugClasses < ActiveRecord::Migration
  def change
    create_table :drugs_drug_classes do |t|
      t.references :drug, index: true
      t.references :drug_class, index: true

      t.timestamps
    end
  end
end
➜  lm git:(DrugClasses) ✗ 

I have not run rake db:migrate. If I've done it wrong, how do I roll back to the right spot? Just delete the files created? I could just trash the branch as you see I just created it.

Thanks in advance!

_____UPDATE______

Ok I destroyed the previous (rails destroy model Drugs_Drug_Classes) and instead did this:

rails g migration CreateJoinTableDrugDrugClass drug drug_class

and it generated this: class CreateJoinTableDrugDrugClass < ActiveRecord::Migration def change create_join_table :drugs, :drug_classes do |t| # t.index [:drug_id, :drug_class_id] # t.index [:drug_class_id, :drug_id] end end end

  1. Why are the indices commented out? I ran the migration and the indexes were actually created (I see them in the schema). That's confusing!

  2. Why doesn't this migration add associations to the Drug and DrugClass models?!

Darby
  • 769
  • 1
  • 8
  • 26
  • You write you read migration guide, but do you read this guide? http://edgeguides.rubyonrails.org/association_basics.html – mikdiet May 02 '13 at 19:48
  • Btw, if you migrate, you should rollback with `rake db:rollback STEP=3` before deleting your 3 migrations – mikdiet May 02 '13 at 19:49
  • 1
    With has_many :through, the association table has an attribute id for each row so you can access it, and could have other attributes. The has_and_belongs_to_many only has the id of the 2 elements it unites. You can run rails d migration etc... to delete a generated migration. – Ghar May 02 '13 at 19:51
  • Yep, Mike I did read that. It also assumes you already have models. thanks. – Darby May 02 '13 at 23:08
  • Thanks for the clarification, Ghar, so I do want the has and belongs to many because it's solely for uniting the 2 elements. – Darby May 02 '13 at 23:09
  • So I migrated what you see above and I get belongs to associations in the join table, and no associations in the drug and drug class tables. – Darby May 02 '13 at 23:15
  • hmm comments are ugly. guess I'll update the main question. please look there. – Darby May 02 '13 at 23:38

0 Answers0