Skip to content

Instantly share code, notes, and snippets.

When you want to create an object and save it in one fell swoop, you can use the create method. Use it now to create another article:
>> Article.create(:title => "Some Fancy Title")
=> #<Article id: 4, title: "Some Fancy Title" >
Instead of returning true or false, the create method returns the object it created - in this case, an Article object. You're actually passing a hash of attributes to the create method. Although hashes are normally surrounded by curly braces, when a hash is only argument to a Ruby method, the braces are optional. You can just as easily create the attributes hash first and then give that to create:
>> attributes = { :titile => "Some other Fancy Title }
>> Article.create(attributes)
=> #<Article id: 5, title: "Some other Fancy Title" >
@waseem
waseem / Gemfile
Created April 25, 2011 12:54 — forked from lifo/Gemfile
gem 'cramp'
gem 'erubis', '2.6.5'
gem 'usher', "0.6.0"
resources :meetings, :only => [:index] do
resources :bookings
end
resources :bookings
@waseem
waseem / gist:1366612
Created November 15, 2011 10:00 — forked from mikebaldry/gist:1366593
find subclasses
def self.all
results = []
ObjectSpace.each_object(Class) do |c|
(results << c) if c < PullSource
end
results
end
class Company < ActiveRecord::Base
###company validation
validates_presence_of :company_name, :message => "Company Name is required!", :unless => :user_name_present?
def user_name_present?
self.user_name.present?
end
end
#!/usr/bin/env ruby
require 'yard'
require 'json'
# Replace YARD's default with our own
template_path = File.expand_path(File.dirname(__FILE__) + "/templates")
YARD::Templates::Engine.template_paths = [template_path]
YARD::Registry.load(Dir["rails/act*/lib/**/*.rb"])
var states = [
'showingAddRelative',
'showingForm',
'showingComplexRelationshipHelp',
'findingExistingRelatives',
'choosingNewOrExisting',
'addingRelative',
'gettingSuggestions',
'showingSuggestions',
'approvingSuggestions',
@waseem
waseem / gist:3220182
Created July 31, 2012 20:26 — forked from gotjosh/gist:3220028
How can I avoid to create the array on the first line?
@vidoes = @publisher.sources.map do |s|
s.videos.paginate(:per_page => params[:limit] ||= 10, :page => params[:page] ||= 1)
end
@waseem
waseem / cars_controller.rb
Created August 17, 2012 09:13
Controller for Card rendering
def show
@card = Card.find(params[:id])
if (@card)
respond_to do |format|
format.xml
format.html
end
end
end
@waseem
waseem / cards_controller.rb
Created August 17, 2012 10:03
CardsController
class CardsController < InheritedResources::Base
require 'aws/s3'
require 'builder'
def show
@card = Card.find(params[:id])
if (@card)
respond_to do |format|
format.xml #{ render @card, :template => '#{Rails.root}/app/views/cards/show.xml.builder', :type => :builder, :layout => false }
format.html