ECのウェブ担当者のメモ

ECサイトを運営管理している、WEB担当プログラマのメモ

スポンサーリンク

Awesome Nested Setのチートシート

f:id:jun9632:20170130164755p:plain

こちらRailsのカテゴリを階層で管理してくれるGem Awesome Nested Setです

github.com

そのチートシートです。

github.com

主に使いそうなところ

これで一気にループ回せます。

Category.each_with_level(Category.root.self_and_descendants) do |category, level|
  ...
end

使いそうなメソッド一覧

my_cat.root                  root for this node
my_cat.level                 the level of this object in the tree (e.g. root = 0)
my_cat.parent                the node's immediate parent
my_cat.children              array of immediate children (just those in the next level)
my_cat.ancestors             array of all parents, parents' parents, etc, excluding self
my_cat.self_and_ancestors    array of all parents, parents' parents, etc, including self
my_cat.siblings              array of brothers and sisters (all at that level), excluding self
my_cat.self_and_siblings     array of brothers and sisters (all at that level), including self
my_cat.descendants           array of all children, children's children, etc., excluding self
my_cat.self_and_descendants  array of all children, children's children, etc., including self
my_cat.leaves                array of all descendants that have no children
my_cat.root?                         true if this is a root node
my_cat.child?                        true if this is a child node (i.e. it has a parent)
my_cat.is_ancestor_of?(obj)          true if nested by any obj
my_cat.is_or_is_ancestor_of?(obj)    true if nested by any obj or self is obj
my_cat.is_descendant_of?(obj)        true if self is nested under obj
my_cat.is_or_is_descendant_of?(obj)  true if self is nested under obj or self is obj
my_cat.leaf?                         true if this is a leaf node (i.e. it has no children)

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 5 超入門

Ruby on Rails 5 超入門

GitHubでForkしたリポジトリをmargeする

f:id:jun9632:20170112104812p:plain

下記の様に、cloneして作ったリポジトリに対して、親のmyapp.gitをmargeする方法です。

$ git clone git@bitbucket.org:xxxxxx/myapp.git

org_originという名前で、親のリポジトリを参照できるようにします。

$ git remote add org_origin git@bitbucket.org:xxxxxx/myapp.git

後は, fetchとmergeすれば完了です

$ git fetch org_origin
$ git merge org_origin/master

関連記事

marketing-web.hatenablog.com

参考記事

ありがとうございました

http://qiita.com/xtetsuji/items/555a1ef19ed21ee42873

RubyでStringを配列に変換する

f:id:jun9632:20161201164715p:plain

rubyでStringを配列に変換する方法です。

使う関数は

split(pattern)

です。

たとえば

080-1234-5678

tel = '080-1234-5678'
tel.split('-')

すると

["080", "1234", "5678"]

という感じに配列に変換してくれます。

配列に分割出来なかったら

例えば、

“00-00"を'-‘で配列にすると

"00-00".split('-')

以下のような結果になりますが

["00", "00"]

もし、"00-00"を'=‘で分割しようとすると

"00-00".split('=')

結果は、以下のように、その文字列自体を配列に入れて返してくれます

["00-00"]

正規表現を使って分割

これまで、文字列を使って分割してきましたが、正規表現を使って分割することもできます。

’32145617741345’なんて文字列を、'1'で分割するときに

'32145617741345'.split('1')

と書いてもいいですが、

'32145617741345'.split(/1/)

としても、以下のような結果が取得できます。

["32", "456", "774", "345"]

今回の例だと単純過ぎるので、そんな使い方はあまりしないと思いますが、 もっと複雑なパターンで分割したいときなどは、正規表現も有効に使えると思います。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

恋するプログラム―Rubyでつくる人工無脳 (プレミアムブックス版)

恋するプログラム―Rubyでつくる人工無脳 (プレミアムブックス版)

AWS EC2のAmazonLinuxでタイムゾーンをUTCから日本時間に変更する

f:id:jun9632:20161021123921p:plain

作業は主に2つ

  • /etc/sysconfig/clockの編集
  • /etc/localtime のリンク先に東京の時間帯ファイルを設定

/etc/sysconfig/clockの編集

/etc/sysconfig/clockに設定されている タイムゾーンをUTC から Asia/Tokyo に変更します

vim /etc/sysconfig/clock
ZONE="Asia/Tokyo"
UTC=true

/etc/localtime のリンク先に東京の時間帯ファイルを設定

/etc/localtime と時間帯ファイル(Asia/Tokyo)にシンボリックリンクを作成する。

sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

きっとこれでよいはず。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Amazon Web Services パターン別構築・運用ガイド

Amazon Web Services パターン別構築・運用ガイド

参考サイト

docs.aws.amazon.com

RailsのSTI(Single Table Inheritance)を無効にする

f:id:jun9632:20160816174108p:plain

RailsのActiveRecordでtypeカラムがあると、STI(Single Table Inheritance)としてテーブルを使おうとします。

一つのテーブルでtypeの値によって複数のモデルが共有する感じです。

これ、とても便利なんですが、意図せずtypeカラムがあってもSTIとして扱います。

それのSTIを無効化する方法です

class Item < ActiveRecord::Base


  self.inheritance_column = :_type_disabled # 追加


end

以上です。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 4 アプリケーションプログラミング

Ruby on Rails 4 アプリケーションプログラミング

RailsのActiverecordでkeyがid ValueがmodelオブジェクトのHashを作りたい

f:id:jun9632:20160815110531p:plain

簡易的にN+1問題に対応しようとした時に、 Keyがid, ValueがそのモデルのオブジェクトのHashが欲しい時がありました。

そんな時は、index_byが良いっぽいです。

たとえば、Itemモデルで下記のようなテーブルがあって

id name code
1 あああ aaaa
2 いいい bbbb
3 ううう cccc

以下のようなハッシュが作りたい時

{1=>#<Item id: 1, name: 'あああ', code: 'aaaa'>, 2=>#<Item id: 1, name: 'あああ', code: 'aaaa'>, ・・・・}

下記のように index_byを使うと取得できます。

Item.all.index_by(&:id)

以上

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Railsレシピブック 183の技

Railsレシピブック 183の技

参考サイト

index_by (Enumerable) - APIdock

Railsで複数チェックボックをstrong parametersで取得する

f:id:jun9632:20160812171332p:plain

Railsでチャックボックスのフォームを使おうとすると毎回迷走します。

複数チェックボックを設定してそれを、controller側でStrong parametersで取得する方法です。

ItemのidをチェックボックスのValueに設定しています。

view

(省略)
- @items.each do |item|
  = check_box_tag'items[id][]', item.id
(省略)

controller

  def item_id_params
    params.require(:items).permit(id: [])
  end

以上、このような感じです。

関連記事

marketing-web.hatenablog.com

marketing-web.hatenablog.com

Ruby on Rails 4 アプリケーションプログラミング

Ruby on Rails 4 アプリケーションプログラミング