Awesome Nested Setのチートシート
こちらRailsのカテゴリを階層で管理してくれるGem Awesome Nested Setです
そのチートシートです。
主に使いそうなところ
これで一気にループ回せます。
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)
関連記事
- 作者: 掌田津耶乃
- 出版社/メーカー: 秀和システム
- 発売日: 2016/12/17
- メディア: 単行本
- この商品を含むブログを見る
GitHubでForkしたリポジトリをmargeする
下記の様に、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
関連記事
参考記事
ありがとうございました
RubyでStringを配列に変換する
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"]
今回の例だと単純過ぎるので、そんな使い方はあまりしないと思いますが、 もっと複雑なパターンで分割したいときなどは、正規表現も有効に使えると思います。
関連記事
恋するプログラム―Rubyでつくる人工無脳 (プレミアムブックス版)
- 作者: 秋山智俊
- 出版社/メーカー: マイナビ出版
- 発売日: 2016/11/28
- メディア: 単行本
- この商品を含むブログを見る
AWS EC2のAmazonLinuxでタイムゾーンをUTCから日本時間に変更する
作業は主に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
きっとこれでよいはず。
関連記事
Amazon Web Services パターン別構築・運用ガイド
- 作者: NRIネットコム株式会社,佐々木拓郎,林晋一郎,小西秀和,佐藤瞬
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2015/03/25
- メディア: 大型本
- この商品を含むブログ (2件) を見る
参考サイト
RailsのSTI(Single Table Inheritance)を無効にする
RailsのActiveRecordでtypeカラムがあると、STI(Single Table Inheritance)としてテーブルを使おうとします。
一つのテーブルでtypeの値によって複数のモデルが共有する感じです。
これ、とても便利なんですが、意図せずtypeカラムがあってもSTIとして扱います。
それのSTIを無効化する方法です
class Item < ActiveRecord::Base self.inheritance_column = :_type_disabled # 追加 end
以上です。
関連記事
Ruby on Rails 4 アプリケーションプログラミング
- 作者: 山田祥寛
- 出版社/メーカー: 技術評論社
- 発売日: 2014/04/11
- メディア: 大型本
- この商品を含むブログ (5件) を見る
RailsのActiverecordでkeyがid ValueがmodelオブジェクトのHashを作りたい
簡易的に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)
以上
関連記事
- 作者: 高橋征義,諸橋恭介
- 出版社/メーカー: ソフトバンククリエイティブ
- 発売日: 2008/05/31
- メディア: 単行本
- 購入: 37人 クリック: 567回
- この商品を含むブログ (92件) を見る
参考サイト
Railsで複数チェックボックをstrong parametersで取得する
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
以上、このような感じです。
関連記事
Ruby on Rails 4 アプリケーションプログラミング
- 作者: 山田祥寛
- 出版社/メーカー: 技術評論社
- 発売日: 2014/04/11
- メディア: 大型本
- この商品を含むブログ (5件) を見る