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
| #/materials/edit.haml | |
| = form_for @material, :url => admin_material_path do |f| | |
| = f.file_field name: 'material[image_ids]' | |
| #material.rb model | |
| attr_accessible :title, :description, :material_type_id, :finish_ids, | |
| :application_ids, :image_ids |
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
| def destroy | |
| @material = Material.find(params[:id]) | |
| @material.destroy | |
| respond_to do |format| | |
| format.html { redirect_to admin_materials_path } | |
| format.json { render json: @material, status: :deleted } | |
| end | |
| end |
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
| def destroy | |
| @material = Material.find(params[:id]) | |
| @material.destroy | |
| respond_to do |format| | |
| format.html { redirect_to admin_materials } | |
| format.json { render json: @material, status: :deleted } | |
| end | |
| end |
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
| mat1 = Material.create!(title: 'Antique Yangtze Limestone', description: 'test desc') | |
| image1 = Image.create!(orig_filename: 'test1.jpg') | |
| mat1.default_image_id = image1 if mat1.default_image_id.nil? | |
| mat1.save! |
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
| # MaterialFinish is my join table | |
| mfs = MaterialFinish.all | |
| MaterialFinish Load (0.5ms) SELECT "material_finishes".* FROM "material_finishes" | |
| => [#<MaterialFinish id: 8, material_id: 3, finish_id: 7>, #<MaterialFinish id: 9, material_id: 3, finish_id: 8>] | |
| mfs.each do |mf| mf.delete end | |
| SQL (21.8ms) DELETE FROM "material_finishes" WHERE "material_finishes"."id" = 8 | |
| SQL (1.8ms) DELETE FROM "material_finishes" WHERE "material_finishes"."id" = 9 | |
| => [#<MaterialFinish id: 8, material_id: 3, finish_id: 7>, #<MaterialFinish id: 9, material_id: 3, finish_id: 8>] |
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
| Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass | |
| SQLite3::SQLException: no such column: material_finishes.: DELETE FROM "material_finishes" WHERE "material_finishes"."" = ? | |
| (0.1ms) rollback transaction | |
| ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: material_finishes.: DELETE FROM "material_finishes" WHERE "material_finishes"."" = ? | |
| from /Users/phong/.rvm/gems/ruby-1.9.3-p0@rails3.1.3/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize' |
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
| class Material < ActiveRecord::Base | |
| has_one :material_type | |
| has_many :material_finishes, :dependent => :destroy | |
| has_many :finishes, :through => :material_finishes | |
| validates :title, presence: true | |
| validates_uniqueness_of :title | |
| end | |
| class Finish < ActiveRecord::Base |
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
| class Material < ActiveRecord::Base | |
| has_many :product_materials | |
| has_many :products, :through => :product_materials | |
| #validates :title, presence: true | |
| end | |
| class Product < ActiveRecord::Base | |
| has_many :product_materials | |
| has_many :materials, :through => :product_materials |
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
| 1.9.3-p0 :001 > p = Product.create(title: "Test Product 1") | |
| (0.1ms) begin transaction | |
| SQL (62.0ms) INSERT INTO "products" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 26 Feb 2012 01:41:35 UTC +00:00], ["title", "Test Product 1"], ["updated_at", Sun, 26 Feb 2012 01:41:35 UTC +00:00]] | |
| (23.4ms) commit transaction | |
| => #<Product id: 1, title: "Test Product 1", created_at: "2012-02-26 01:41:35", updated_at: "2012-02-26 01:41:35"> | |
| 1.9.3-p0 :002 > m = Material.create(title: "Iron") | |
| (0.1ms) begin transaction | |
| SQL (0.7ms) INSERT INTO "materials" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 26 Feb 2012 01:41:43 UTC +00:00], ["title", "Iron"], ["updated_at", Sun, 26 Feb 2012 01:41:43 UTC +00:00]] | |
| (44.0ms) commit transaction | |
| => #<Material id: 1, title: "Iron", created_at: "2012-02-26 01:41:43", updated_at: "2012-02-26 01:41:43"> |
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 |