Skip to content

Instantly share code, notes, and snippets.

View deJaVisions's full-sized avatar

deJaVisions deJaVisions

View GitHub Profile
@deJaVisions
deJaVisions / gist:1964328
Created March 3, 2012 04:20
error: Can't mass-assign protected attributes: {:name=>%22material
#/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
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
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
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!
# 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>]
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'
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
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
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">
@deJaVisions
deJaVisions / gist:1912024
Created February 26, 2012 01:12
can't get p = Product.first; p.materials to work without erroring out using rails 3.2
# 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