Нистратов Артём
anistratov@go-promo.ru
rails g model Snippet title:string content:textrails g scaffoldgenerator
model name
attribute name
attribute type
code = Snippet.new title: 'Test', content: 'a = :foo'code.new_record?
code.savecode.idcode.persisted?rails consoleCreate
h = {}h = Hash.newh[:foo] = 'bar'h[:foo]'bar'h[:baz]nilh['key'] = 1Hash
Snippet.new title: 'Test'
Snippet.new(title: 'Test')params = { title: 'Test' }
Snippet.new paramsSnippet.new({ title: 'Test' })Method calls
code = Snippet.new
code.saveSnippet.creategit add -Agit commit -m "add Snippet model"git checkout -b snippetsnew branch name
git push origin snippetsserver
branch
Snippet.find(1)Snippet.where(title: 'Test').firstSnippet.select([:content])
.where(title: 'Test')
.order(:created_at)
.limit(1)Read
Snippet.limit(10).pluck(:content)Query API
.where(is_draft: false).where('is_draft = ?', false).where('is_draft = :draft',
draft: false).where("is_draft = #{params[:draft]}")rails g model User name:stringrails g migration AddUserIdToSnippet user_id:integerrake db:migrateRelations
class User < ActiveRecord::Base
has_many :snippets
endclass Snippet < ActiveRecord::Base
belongs_to :user
endSnippet.joins(:user)
.where(user: { name: 'John' })
.countUser.joins(snippets: :languages)
.where(languages: { name: 'Ruby' })
.take(10)rails g model Language name:string
rails g migraion CreateSnippetsLanguages \
snippet_id:integer language_id:integerhas_and_belongs_to_many :snippetsUpdate
snippet.content = 'Some information'snippet.savesnippet.updated_atsnippet.update_attributes(title: 'Yay')snippet.assign_attributes(content: '._.')snippet.changedsnippet.changesDelete
snippet.destroySnippet.destroy(ids)Snppet.where(title: 'Test').destroy_allSnippet.where(title: nil).each { |post| post.destroy }Snippet.where(...).find_each { |code| ... }Enumerator
[1, 2, 3].each { |i| puts i * 2 }[1, 2, 3].each do |i|
puts i * 2
endarray.map { |value| ... }
.sort.reverse.with_index
.reduce do |result, (value, index)|
...
end