Skip to content

Instantly share code, notes, and snippets.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python2.7-dev python3-dev
@Naggi-Goishi
Naggi-Goishi / convertion.rb
Last active December 5, 2017 14:15
Convert mysql to rails migration file
require 'date'
class Converter
def initialize(file_name)
@file_name = file_name
@tables = {}
@current_table = {}
end
def to_ruby(rails_v)
@Naggi-Goishi
Naggi-Goishi / file0.rb
Created June 2, 2017 14:35
STIとポリモーフォック関連は相容れない ref: http://qiita.com/Naggi-Goishi/items/8dc6345c4324ae34817d
class User < ActiveRecord::Base
end
class Admin < User
has_many :images, as: :imageable
end
class NormalUser < User
has_many :images, as: :imageable
end
@Naggi-Goishi
Naggi-Goishi / file0.rb
Last active May 10, 2017 13:51
ActiveRecord::QueryMethods#limitとActiveRecord::FinderMethods#take ref: http://qiita.com/Naggi-Goishi/items/856a7698f167efdb2a17
twelves = User.where(age: 12)
# ActiveRecord::QueryMethods#limitを使用する
twelves.limit(3)
# User Load (4.9ms) SELECT `users`.* FROM `users` WHERE `users`.`age` = '12' LIMIT 3
#=> <#ActiveRecord::Relation [<#User id: 1, name: "ゴン=フリークス", age: 12>, <#User id: 2, name: "キルア=ゾルディック", age: 12>, <#User id: 3, name: "ピーター・パン", age: 12>]>
# ActiveRecord::FinderMethods#takeを使用する
twelves.take(3)
@Naggi-Goishi
Naggi-Goishi / beating_freeza.rb
Last active May 10, 2017 15:58
ActiveRecord::Relationのオブジェクトに対してメソッドを追加したい時 ref: http://qiita.com/Naggi-Goishi/items/fa756eb3814847fdd05b
class BeatingFreeza
class << self
def possible?(collection)
new(collection).possible?
end
end
def initialize(collection)
@collection = collection
end
@Naggi-Goishi
Naggi-Goishi / main.rb
Last active May 1, 2017 06:57
Rubyで漢字をひらがなに変換する ref: http://qiita.com/Naggi-Goishi/items/e9f8753e88bc9f0b67b5
require 'mechanize'
AGENT = Mechanize.new
BASE_URL = 'https://yomikatawa.com/kanji/'
def to_hiragana(kanji)
AGENT.get(BASE_URL + kanji).search('#content p').first.inner_text
end
def to_romaji(kanji)
@Naggi-Goishi
Naggi-Goishi / keys_to_sym.rb
Last active February 1, 2017 17:07
meta-programming that add method of keys_to_sym, which convert every keys to symbol type from string.
class Hash
def keys_to_sym
self.inject({}) do |hash, (key, value)|
value = value.keys_to_sym if value.class == Hash
hash[key.to_sym] = value
hash
end
end
end
問題3. Rails記述問題
1. RailsでActiveRecord::Baseを継承すればfind,newなどのDatabaseに関わる様々なメソッドが使えるようになる。
2. mergeメソッドは二つの配列を合成するためのメソッドである。
3. n+1問題とはSQL文を発行しすぎることの問題である。これはcontrollerで取り出した情報のリレーションされているものをviewファイルで取り出すときにもう一度SQL文が走ってしまうことに原因があるので、controllerで取り出すときにincludeメソッドで同時にリレーションするものもひとつのSQL文でまとめて取り出すことで解決する。
4. deviseは会員登録の機能を実装するためのgemである。
5. kaminariを導入すればページネーションを簡単に実装することができる。