创建三个表连接表

时间:2014-03-14 16:08:29

标签: ruby-on-rails pluralize

我正在尝试创建一个三表联合表。我有三张桌子,老师,主题和句号。他们将加入一个表,这是三者的组合。我试图通过执行以下操作来实现此目的:

组合表:

class UserClass < ActiveRecord::Base
  belongs_to :subject
  belongs_to :period
  belongs_to :teacher
end

主题表:

class TestTable < ActiveRecord::Base
    has_many :user_class
    has_many :period, :through => :user_classes
    has_many :teacher, :through => :user_classes
end

老师表:

class Table < ActiveRecord::Base
    validates :name, presence: true, length: { maximum: 50}

    has_many :user_classes
    has_many :subjects, :through => :user_class
    has_many :periods, :through => :user_class
end

期间表:

class TestTableTest 1 < ActiveRecord::Base
    has_many :user_classes
    has_many :subjects, :through => :user_class
    has_many :teachers, :through => :user_class
end

当我这样做创建一个User.create(:period => Period.first, :teacher => Teacher.first, :subject => Subject.first)时,我得到了这个回复:

Period Load (0.2ms)  SELECT "periods".* FROM "periods" ORDER BY "periods"."id" ASC LIMIT 1
  Teacher Load (0.1ms)  SELECT "teachers".* FROM "teachers" ORDER BY "teachers"."id" ASC LIMIT 1
  Subject Load (0.1ms)  SELECT "subjects".* FROM "subjects" ORDER BY "subjects"."id" ASC LIMIT 1
ActiveRecord::UnknownAttributeError: unknown attribute: period
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/attribute_assignment.rb:47:in `rescue in _assign_attribute'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/attribute_assignment.rb:23:in `each'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/core.rb:460:in `init_attributes'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/core.rb:185:in `initialize'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/inheritance.rb:27:in `new'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/inheritance.rb:27:in `new'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:36:in `create'
    from (irb):1
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
    from /Users/mattmoss/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我尝试使用config.active_record.pluralize_table_names = false但是我的所有其他表格都无法使用。

0 个答案:

没有答案