1

I am trying to create a three table joint table. I have three tables, teacher, subject, and period. They would join into one table that is a combination of the three. I am trying to accomplish this by doing the following:

combination table:

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

subject table:

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

teacher table:

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

period table:

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

When I do this to create one User.create(:period => Period.first, :teacher => Teacher.first, :subject => Subject.first) I get this response:

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>'

I have Tried to use config.active_record.pluralize_table_names = false but then all of my other tables don't work.

matthew
  • 467
  • 8
  • 23

0 Answers0