activerecord - rails many to many relationship from the beginning (all model creation) -
i've read lot of questions on , migration guides , api , not clear. of answers assume have models , tables 2 entities. i'm starting scratch need full solution. have drugs , drug classes. drugs can in many drug classes , drug classes have many drugs. what's best way set up? i'd 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 use generate , generate start , edit files created generate command?
i don't difference between has , belongs many , has many through.
i've run far....
switched 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 what's been generated.
➜ lm git:(drugclasses) ✗ cat db/migrate/20130502191118_create_drugs.rb class createdrugs < activerecord::migration def change create_table :drugs |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 |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 |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 wrong, how roll right spot? delete files created? trash branch see created it.
thanks in advance!
_____update______
ok destroyed previous (rails destroy model drugs_drug_classes) , instead did this:
rails g migration createjointabledrugdrugclass drug drug_class
and generated this: class createjointabledrugdrugclass < activerecord::migration def change create_join_table :drugs, :drug_classes |t| # t.index [:drug_id, :drug_class_id] # t.index [:drug_class_id, :drug_id] end end end
why indices commented out? ran migration , indexes created (i see them in schema). that's confusing!
why doesn't migration add associations drug , drugclass models?!
Comments
Post a Comment