Last active
August 29, 2015 14:24
-
-
Save amirrajabi/2d8dbc59e31015d2979b to your computer and use it in GitHub Desktop.
Ruby on Rails Programming syntax
This file contains 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
Routing: | |
get 'demo/index' | |
match ':controller(/:action(/:id))', :via => :get | |
match ':controller(/:action(/:id))', :via => [:get, :post] | |
match ':controller(/:action(/:id(.:format)))', :via => :get | |
root "demo/index" | |
class DemoController < ApplicationController | |
end | |
layout false | |
def index | |
end | |
render('hello') #run other page | |
redirect_to(:controller => 'demo', :action => 'index') | |
redirect_to("amirrajabi.com") | |
<% code %> | |
<%= code %> | |
<%= 1+2 %> =>3 | |
<% target = "world" %> | |
<%= "Hello #{{target}}" %> | |
puts => just render text on consule | |
<% 3.times do %> | |
<%= "AMIR" %> | |
<% end %> | |
variable | |
@instance_variable | |
<% target = "Amir" %> | |
<%= "hello #{target}" %> | |
@array = [1, 2, 3, 4, 5] | |
<% @array.each do |n| %> | |
<%= "#{n}-Rajabi, " %> | |
<% end %> | |
<% 3.times do %> | |
<%= "Amir" %> | |
<% end %> | |
<%= link_to('Hello Page', {:action => 'hello'}) %> | |
<%= link_to('Hello Page With Parameters!', {:action => 'hello', :page => 5, :id => 20}) %> | |
@id = params['id'].to_i | |
@page = params[:page].to_i | |
ID: <%= @id %> | |
Page: <%= params["page"] %> | |
Next page: <%= @page +1 %> | |
.to_i | |
.to.s | |
.to_a | |
MySQL---------------------- | |
$ mysql -u root -p | |
SHOW DATABASES; | |
CREATE DATABASE db_name; | |
USE db_name; | |
DROP DATABASE db_name; | |
GRANT ALL PRIVILEGES ON db_name.* TO 'username'@'localhost' IDENTIFIED BY 'password'; | |
SHOW GRANTS FOR 'username'@'localhost' | |
$ mysql -u username -p db_name | |
$ rake db:schema:dump => for checking ok connection with database | |
$ rake -T => list of all commands | |
$ rake -T db => list of all db commands | |
$ rake db:schema:dump RAILS_ENV = development | |
$ rails g migration TableName | |
$ rails g model Name | |
migrate: | |
class CreateTopTens < ActiveRecord::Migration | |
def up | |
create_table :top_tens do |t| | |
t.timestamps null: false | |
end | |
end | |
def down | |
drop_table :top_tens | |
end | |
end | |
data type : string/text/integer/float/boolean/... | |
:limit | |
:default | |
:null | |
:precision | |
:scale | |
$ rake db:migrate VERSION=0 | |
$ rake db:migrate:status | |
$ rake db:migrate VERSION=120920152230 | |
$ rake db:migrate:up VERSION=120920152230 | |
$ rake db:migrate:down VERSION=120920152230 | |
add_column(table, column, type, option) | |
remove_column(table, column) | |
rename_column(table, column, new_name) | |
change_column(table, column, type, option) | |
add_index(table, column, option) | |
remove_index(table, column) | |
:unique => true/false | |
:name => "your_custom_name" | |
Create Record in Table: | |
user = User.new | |
user.user_name = "Amir" | |
user.save | |
user = User.new(:first_name => "Amir", :last_name => "Rajabi", :username => "arajabi") | |
user.save | |
user = User.create(:first_name => "Amir", :last_name => "Rajabi", :username => "arajabi") | |
Update data on record:------------------- | |
user = User.find(2) | |
user.user_name = "Amir" | |
user.save | |
user = User.find(2) | |
user.update_attributes(:first_name => "amir", :last_name => "Rajabi") | |
Delete record:------------------- | |
user = User.find(2) | |
user.destroy | |
user.svae | |
find:---------------------------- | |
user = User.find(1) | |
user = User.find_by_name("Amir") | |
user = User.all / .first / .last | |
users.each do |user| | |
puts subject.name | |
end | |
User.where(:name => "Amir") | |
User.where("name LIKE ? OR job LIKE ?", ["Amir","Programmer"]) | |
users.to_sql | |
User.where(:name => "Amir").first | |
.order | |
.limit | |
.offset | |
User.order("id ASC/DESC").lomit(20).offset(40) | |
scope :subject, lambda/-> { where(:name => "Amir")} | |
def self.subject | |
where(:name => "Amir") | |
end | |
scope :sorted, -> { where("subject.position ASC")} | |
scope :search, -> { |query| where(["name LIKE ?", "%#{query}%"])} | |
scope :search, -> { where('game_name LIKE :search OR genre LIKE :search OR console LIKE :search', search: "%#{search}%") | |
scope :search, -> { search(keywords) } | |
def self.search keywords | |
return Media.all if keywords.empty? #if field was empty back all content | |
search_fields = BASIC_SEARCH_FIELDS #list of table fields | |
search_array = search_fields.map { |f| "#{f} LIKE ?" } #create new array with "LIKE ?" | |
search_str = search_array.join(' OR ') #Join all cells of array | |
search_values = keywords.map { |k| "%#{k}%"} * search_fields.count #create new array and add keyword * field.length | |
Media.where(search_str, *search_values) # serach in select field on db | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment