豚吐露@wiki
コメントログ
最終更新:
ohden
-
view
- rails generate migration add_index_to_users_email → r db:migrate → vi db/migrate/20170410090808_add_index_to_users_email.rb → db:migrate で反映されずにハマる...orz -- (s1n) 2017-04-11 10:24:23
- RailsTutorial: リスト6.29
test/fixtures/ はtest前に投入されるdata。testの内容に関わらず必ず投入されるらしい。なので関係無いとこで一意制約エラーが出るようになるらしい。 -- (s1n) 2017-04-11 11:23:05 - 7.3.4 演習 -- (s1n) 2017-04-11 18:27:27
- 10.1.2 -- (s1n) 2017-04-17 18:24:09
- cookpadのコーディング規約
https://github.com/cookpad/styleguide/blob/master/ruby.ja.md -- (s1n) 2017-09-28 18:50:54 - http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html -- (s1n) 2017-10-14 22:47:52
- postgresを使う
→gem pgのinstallで失敗
$ sudo apt install libpq-dev
で依存解決 -- (s1n) 2018-04-06 11:20:09 - nodejs update
> curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
> sudo apt-get install -y nodejs -- (s1n) 2018-04-06 11:55:55 - Rails.application.eager_load!
ActiveSupport::DescendantsTracker.descendants ApplicationRecord #model class list
ActiveSupport::DescendantsTracker.descendants ApplicationController #controller class list -- (s1n) 2018-04-23 11:58:21 - 共通のルーティングの追加方法。
> resources :users do
> post :confirm, on: :collection
> end
は
> concern :confirmable { post :confirm, on: :collection }
> resources :users, concerns: :confirmable
とも書ける。concernsへの指定は、配列にして複数指定することも可能。
> resources :users, concerns: [:confirmable] -- (s1n) 2018-08-29 09:43:56 - >has_many hoges
>def hoges
> hogehoge
>end
のように関連名と同じmethodを定義すると、基本的にmethodの方が使われる。
が、他の関連のthroughからは、関連のhogesの方が参照される。らしい... -- (s1n) 2018-09-13 16:31:43 - ActiveRecord絡みのメモ
* scope
* unscoped
* default_scope
* unscope(order: :name) -- (s1n) 2018-09-13 17:01:48 - FactoryGirlで予約語と設定項目名がかぶる場合...
>add_attribute(:factory){ "室蘭工場" } -- (s1n) 2018-09-13 17:35:55 - apply_join_dependency
eager_loadをjoins扱いしてくれる? -- (s1n) 2018-09-13 18:19:27 - postgres+rspecが遅い...
test環境やlocal環境で完結する話なら、`/etc/postgresql/10/main/postgresql.conf`の`fsync = off`としてしまうのも手。
システムコールfsyncはメモリ上とディスク上のfileを同期させる処理。なので、停電などでメモリ上にしかないデータが消えても大丈夫なlocal環境やtest環境ならoffってしまって性能重視にしてしまえと。
当然、本番環境で設定したらやばいことが起こりそうなので注意。 -- (s1n) 2018-09-20 11:43:48 - https://blog.freedom-man.com/activerecord-join-order-limit/ join, order, limitで件数がおかしくなる問題 -- (s1n) 2018-09-25 09:56:24
-
>>http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html
関連の情報を取る方法。
Model.reflect_on_all_associations
関連名だけ欲しかったら
Model.reflect_on_all_associations.map(&:name)
で一通り取れる。
belongs_to関連だけ取得したかったら
Model.reflect_on_all_associations(:belongs_to)
has_many関連だけ取得したかったら
Model.reflect_on_all_associations(:has_many)
なんて条件で絞り込んだりもできる。
関連名を指定してやると対象の情報を取れる
Model.reflect_on_association(:column_name)
なんてのもある。指定した関連が無かったらnilが帰ってくる。
他にもhashがもらえる
Model.reflections
なんてのもある。
-- (s1n) 2018-09-27 14:30:14 - DBにmysqlを選んだ場合、defaultで『collate utf8_unicode_ci』が有効なSQLが吐かれる。
なので、『%ほげ%』でlike検索を行った場合『%ホゲ%』『%ホゲ%』も検索対象になってしまうらしい。 -- (s1n) 2018-10-24 11:21:27 - railsのように意味のないuniqな数字をprimary keyとして割り振ったkeyをサロゲートキーと呼ぶ。 -- (s1n) 2019-11-01 09:55:03
- railsでPostgreSQLを使いたい場合、pgを入れる必要がある。
pgを入れるには、予めPostgreSQLをinstall済でないとダメ。
>$ sudo apt install postgresql -- (s1n) 2020-02-06 12:40:13