Skip to content

Instantly share code, notes, and snippets.

@Panczo
Panczo / index.md
Created February 29, 2016 10:40 — forked from rstacruz/index.md
Rails models cheatsheet

Rails Models

Generating models

$ rails g model User

Associations

belongs_to

has_one

#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@Panczo
Panczo / 01_step.sh
Last active August 29, 2015 14:06 — forked from Mikke/01_step.sh
rails g scaffold Book title:string description:text
rails g scaffold Pubhouse title:string address:text
rails g model BookPubhouse book_id:integer pubhouse_id:integer
rake db:migrate
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
@Panczo
Panczo / csv import
Last active August 29, 2015 14:05
Import from csv file
require 'csv'
CSV.foreach("test.csv") do |row|
username = row[0]
id = row[1]
roles = row[2..-1]
puts "User: #{username} id: #{id} has following role:"
roles.each do |r|
puts r