Created
February 26, 2012 01:12
-
-
Save deJaVisions/1912024 to your computer and use it in GitHub Desktop.
can't get p = Product.first; p.materials to work without erroring out using rails 3.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # see bottom for rails console example | |
| # models/product.rb | |
| class Product < ActiveRecord::Base | |
| has_many :products_materials | |
| has_many :materials, :through => :products_materials | |
| end | |
| # models/material.rb | |
| class Material < ActiveRecord::Base | |
| has_many :products_materials | |
| has_many :products, :through => :products_materials | |
| end | |
| # models/products_material.rb (join model | |
| class ProductsMaterial < ActiveRecord::Base | |
| belongs_to :product | |
| belongs_to :material | |
| end | |
| ### migrations: | |
| class CreateProducts < ActiveRecord::Migration | |
| def change | |
| create_table :products do |t| | |
| t.string :title | |
| t.timestamps | |
| end | |
| end | |
| end | |
| class CreateMaterials < ActiveRecord::Migration | |
| def change | |
| create_table :materials do |t| | |
| t.string :title | |
| t.timestamps | |
| end | |
| end | |
| end | |
| class CreateProductsMaterial < ActiveRecord::Migration | |
| def change | |
| create_table :products_material :id => false do |t| | |
| t.integer :product_id | |
| t.integer :material_id | |
| end | |
| end | |
| end | |
| Loading development environment (Rails 3.2.1) | |
| 1.9.3-p0 :002 > p = Product.create(title: 'test prod 1') | |
| (0.1ms) begin transaction | |
| SQL (28.8ms) INSERT INTO "products" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 26 Feb 2012 01:16:56 UTC +00:00], ["title", "test prod 1"], ["updated_at", Sun, 26 Feb 2012 01:16:56 UTC +00:00]] | |
| (1.3ms) commit transaction | |
| => #<Product id: 1, title: "test prod 1", created_at: "2012-02-26 01:16:56", updated_at: "2012-02-26 01:16:56"> | |
| 1.9.3-p0 :003 > m = Material.create(title: 'gold') | |
| (0.1ms) begin transaction | |
| SQL (0.6ms) INSERT INTO "materials" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 26 Feb 2012 01:17:07 UTC +00:00], ["title", "gold"], ["updated_at", Sun, 26 Feb 2012 01:17:07 UTC +00:00]] | |
| (2.7ms) commit transaction | |
| => #<Material id: 1, title: "gold", created_at: "2012-02-26 01:17:07", updated_at: "2012-02-26 01:17:07"> | |
| 1.9.3-p0 :004 > p.materials | |
| NameError: uninitialized constant Product::ProductsMaterial | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/inheritance.rb:119:in `compute_type' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/reflection.rb:172:in `klass' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/reflection.rb:385:in `block in source_reflection' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/reflection.rb:385:in `collect' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/reflection.rb:385:in `source_reflection' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/reflection.rb:508:in `check_validity!' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations/association.rb:26:in `initialize' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:24:in `initialize' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations/has_many_through_association.rb:10:in `initialize' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations.rb:157:in `new' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations.rb:157:in `association' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/activerecord-3.2.1/lib/active_record/associations/builder/association.rb:44:in `block in define_readers' | |
| from (irb):4 | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start' | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>' | |
| from script/rails:6:in `require' | |
| from script/rails:6:in `<main>'1.9.3-p0 :005 > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment