LoginSignup
1
0

More than 5 years have passed since last update.

Rails

Last updated at Posted at 2017-02-21

0001

Limit vs Take

  • take return array
  • limit return ActiveRecord relation which can used to chain

How to Use CSV in ruby for write to file


require 'csv'

HEADERS = ['x', 'y', 'z']

CSV.open('file', 'w', headers: HEADERS) do |csv|
    csv << [x, y, z]
end

# or

test = CSV.open('file', 'w', headers: HEADERS)
test.puts [x, y, x]
test.close

present? vs blank? (nil?, empty?)

  • nil? 単純にnilか
  • empty? は[], {}, '', ""を判断 not nil
  • blank?は nil? + blank?
  • present?は blank?の逆、でもonly for ruby

0003

Rails単一継承(STI)とサブdir構成を実現する方法

方法1

app/models/parent.rb
app/models/parent/mother.rb
app/models/parent/father.rb

class Parent < ApplicationRecord; end
class Parent::Mother < Parent; end
class Parent::Father < Parent; end

# rails consoleで検索するには
Parent::Mother.all
Parent.where(type: 'Parent::Mother')

方法2

# add this to config/application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'models', 'parent')]

app/models/parent.rb
app/models/parent/mother.rb
app/models/parent/father.rb

class Parent < ApplicationRecord; end
class Mother < Parent; end
class Father < Parent; end

# rails consoleで検索するには
Mother.all
Mother.where()

0004

development method

0005

  • Real time application implement example
  • Webpack separate front from back explain
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0