豚吐露@wiki
Rails
最終更新:
ohden
-
view
Ruby on Rails
Ubuntu 10.04
Ruby 1.8.7
RubyGems 1.3.7
Ruby 1.8.7
RubyGems 1.3.7
install
~tips~
RAILS_ENV
ほとんどのlinuxでは、実行commandの前に『【環境変数名】=【設定値】』と記載することで、そのcommandでのみ有効な環境変数を設定できる。
RAILS_ENV=development bin/rails db:migrate
RAILS_ENV=production bin/rails db:migrate
RAILS_ENV=test bin/rails db:migrate
port 3000を通す
CentOS 7.3
$ sudo firewall-cmd --permanent --add-port=3000/tcp
success
『--permanent』は設定を恒久的に行いたい場合に設定。
設定をreload。
$ sudo firewall-cmd --reload
success
更新日: 2022年09月23日 (金) 23時04分10秒
- >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