Rails 3 - has_and_belongs_to_many

早过忘川 提交于 2019-12-02 14:03:31

问题


I have 2 models - Teacher and Subject. A want to connect them via Join table with name Qualification.

It looks like i do something wrong:

class Teacher < ActiveRecord::Base
  has_and_belongs_to_many :subjects, :join_table => "Qualification"
end

class Subject < ActiveRecord::Base 
  has_and_belongs_to_many :teachers, :join_table => "Qualification"
end

My migration:

class CreateQualificationJoinTable < ActiveRecord::Migration
  def change
    create_table :qualification, :id => false do |t|
      t.integer :subject_id
      t.integer :teacher_id
    end
    add_index :qualification, :subject_id
    add_index :qualification, :teacher_id
  end
end

When i open rails console and print, for example

ruby-1.9.3-head :013 > Qualification

I get this:

NameError: uninitialized constant Qualification
    from (irb):13
    from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
    from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
    from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

What is wrong?


回答1:


First, creating the table in a migration doesn't define your model. You have to create a Qualification model in app/models/qualification.rb:

class Qualification < ActiveRecord::Base
  belongs_to :subjects
  belongs_to :teachers
end

Second, if you're using Rails 3, throw out has_and_belongs_to_many and use has_many :through:

class Teacher < ActiveRecord::Base
  has_many :qualifications
  has_many :subjects, :through => :qualifications
end

class Subject < ActiveRecord::Base 
  has_many :qualifications
  has_many :teachers, :through => :qualifications
end



回答2:


You should only use has_and_belongs_to_many if you don't intend to work directly with the join table. In your case, use it if you don't intend to use Qualification itself but only Teacher and Subject and let Active Record do the dirty work. If you have anything to do with the join model, use has_many :through.

Read more here: Choosing Between has_many :through and has_and_belongs_to_many




回答3:


In rails 3 the best way is make intermediate table qualification through migration

the attributes will be like of this table

subject_id:integer teacher_id:integer

and also make class of qualification like this

class Qualification < ActiveRecord::Base
  has_many :subjects
  has_many :teachers
end

and then define other two models like

 class Teacher < ActiveRecord::Base
   has_many :qualifications
   has_many :subjects, :through => :qualifications
 end

 class Subject < ActiveRecord::Base 
  has_many :qualifications
  has_many :teachers, :through => :qualifications
 end

and read this link carefully also

   http://blog.hasmanythrough.com/2007/1/15/basic-rails-association-cardinality


来源:https://stackoverflow.com/questions/9944542/rails-3-has-and-belongs-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!