Skip to content

Instantly share code, notes, and snippets.

View metasoarous's full-sized avatar
💻

Christopher Small metasoarous

💻
View GitHub Profile
class Cake < ActiveRecord::Base
has_many :layers
end
class Layers < ActiveRecord::Base
belongs_to :cake
belongs_to :creation_setting, :polymorphic => true
end
class IceCreamLayerSetting < ActiveRecord::Base
#create the rails project using mysql as db instead of sqlite
rails -d mysql ProjectName
#set up db
mysqladmin -u root create project_name_development
mysqladmin -u root create project_name_test
...
#Do all yer configuring stuff with for db in config/database.yml
params = {:thickness => "2 inches",
:comments_attributes => [{:text => "the silly string"},
{:text => "the silly putty"}]}
Layer.new(params)
#=> ActiveRecord::UnknownAttributeError: unknown attribute: comments_attibutes
params = {:thickness => "4 cm",
:creation_setting_type => "PhylloLayerSetting",
:creation_setting_attributes => {:bake_time => "24 min"}}
Layer.new(params)
#=> NoMethodError: undefined method `build_creation_setting' for #<Layer:0x2420bc8>
def build_creation_settings(params)
type = self.attributes["creation_setting_type"]
#create an array with the name of all application models
models = []
Dir.glob( RAILS_ROOT + '/app/models/*' ).each do |f|
models << File.basename( f ).gsub( /^(.+).rb/, '\1').camelize
end
#check to make sure that the type is actually a model before passing it
#through the scarriness of eval()
if models.include? type
attr_accessor :creation_setting_attributes
def comments_attributes=(attributes)
attributes.each { |att| self.comments.build(att) }
end
class TasksController < ApplicationController
def create
@task = Task.new(params[:task])
respond_to do |format|
# If all is well, clear out the new_task store and redirect to the project
if @task.save
session[:new_task] = nil
flash[:notice] = 'Task added successfully'
format.html { redirect_to @task.project }
# If something goes south, save the task that is being worked on
(in /Users/cts/code/sharp-logic)
rake aborted!
undefined method `feature_pattern=' for #<Cucumber::Rake::Task:0x1015dbfb8>
gem install cucumber -v 0.3.104
@metasoarous
metasoarous / gist:1081500
Created July 13, 2011 22:40
assign_nested_attributes_for_one_to_one_association method definition
# File activerecord/lib/active_record/nested_attributes.rb, line 289
def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
options = nested_attributes_options[association_name]
attributes = attributes.with_indifferent_access
check_existing_record = (options[:update_only] || !attributes['id'].blank?)
if check_existing_record && (record = send(association_name)) &&
(options[:update_only] || record.id.to_s == attributes['id'].to_s)
assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)