よしだです

プログラミングの勉強してます

この前のherokuで出たエラーの原因

ただの僕のミスでした。てへへ。

 

データのアソシエーションでdependent: :destroyを指定していなかったせいで、このデータdestroyするとアソシエーションが変なことになるんだけど!?!?って感じでエラーが発生してたっぽいですね。

 

みんなもhas_manyとかしたらちゃんとdependent: :destroyもしよう!

 

 

herokuつらい

herokuに新しくデプロイすると毎回なんかしらのエラーが出て参ります

 

今回はwe're  sorry, but something went wrongってエラーが出たんですが、これはどうやらDB周りのエラーっぽいですね

 

普通にmigrateとかはしておいたんですが、特定の動作にだけ出てくるので、データベースをリセットしてmigrateし直したらなおりました。謎。

 

たぶん一回目のmigrateはなぜかうまく出来てなかったっぽいですね。

gemfile.lockがcommitされてなくてherokuにデプロイできなかった話

普通にherokuにデプロイしようとしたら

       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       You are trying to install in deployment mode after changing
       your Gemfile. Run `bundle install` elsewhere and add the
       updated Gemfile.lock to version control.
       You have added to the Gemfile:
       * pg

とかいうエラーが出て怒られた。

 

ググってみるとgemfileとgemfile.lockが不整合になっていると起こるエラーらしいので、改めてコミットし直したら解決しました。

 

なんでgemfile.lockだけコミットできてなかったんだろうなあ

bundle installやる前にコミットしてたってことなんだろうけど、無意識っていやですねえ

cssで文章をwidth内で折り返す方法

あああああああああああああああああああああああああああああ

 

と入力された文章をそのまま表示させようとしたら、折り返さずいつまでも横に伸び続けて笑ってしまったのでメモ。

 

折り返しさせるためには

 

word-wrap: break-word;

 

というCSSが必要でした。

 

 

belongs_toでclass_nameを指定した時の命名規則変化について

has_many throughでUserモデル、communities_usersモデル、Communityモデルがあった場合

 

中間モデルに

Belongs_to :user

Belongs_to :community

と書いた場合と

Belongs_to :user_id, class_name: "User"

Belongs_to :community_id, class_name: "Community"

と書いた場合で命名規則が変化するようです

 

具体的には

 

Belomgs_to :userと書いた場合、Userモデルでは

 

has_many :communities_users

has_many :communities, through: :communities_users, source: :community

と書けばいいですが

 

Belongs_to :user_id, class_name: "User"と書いた場合は

 

has_many :communities_users

has_many :communities, through: :communities_users, source: :community_id

と書く必要があるようです。

railsのアソシエーションでAssociationTypeMismatchエラーが出てハマった

データベースのリレーションが苦手です。吉田です。

 

railsのアソシエーションで軽くハマったのが解決したので、メモ。

 

今回の原因は命名規則が間違っていてエラーが起こりました。

 

作りたかったのは、ユーザーはたくさんのコミュニティを持っていて、コミュニティもたくさんのユーザーを持っている関係。

そして、コミュニティは一人の管理ユーザーに従属している関係です。

 

とりあえずモデルを作ります。

Userは

id

name

 

Communityは

id

name

admin_user

 

中間テーブルのCommunity_usersに(名前付け間違えました)

user_id

community_id

 

これでUserモデルに

has_many :community_users

has_many :communities, through: :community_users,  source: :community_id

has_many :admin_circles, class_name: "Circle", foreign_key: "admin_user"

 

Community_usersモデルに

belongs_to :user_id, class_name: "User"

belongs_to :community_id, class_name: "Community"

 

Communityモデルに

has_many :community_users

has_many :users, through: :community_users, source: :user_id

belongs_to :admin_user, class_name: "User"

 

これで、僕の予想では

@user.admin_circles.buildみたいにやればadmin_userカラムにユーザーのIDが入った状態でbuild出来ると思ったのですが、タイトルのAssociationTypeMismatchというエラーを吐きました。

 

軽くググると、rails命名規則に適合していないと出てくるエラーらしいので、適合してなさそうな所を探す。

とりあえずCommunityモデルのadmin_userカラムをadmin_user_idに変更して、

Userモデルの

has_many :admin_circles, class_name: "Circle", foreign_key: "admin_user"

has_many :admin_circles, class_name: "Circle", foreign_key: "admin_user_id"

に変更。

 

もう一度@user.admin_circles.buildをやると無事成功。

 

ソースコードとか読んでもうちょっと理解深めたいですな。

 

footerを最下部へ設置する方法

空のページのようにコンテンツがない状態でもfooterを画面の一番下に設置する方法(fixedではなく)はないかなと思い調べました。

 

すると

html { 

 position: relative;

 min-height: 100%;

}

 

footer {

 position: absolute;

 bottom: 0;

}

 

とすると可能なようですね。

これにプラスしてページ全体にfooterと同じ高さのマージンをとると良いとのことです。

 

 

参考リンク

CSSだけで実装する!短めページの隙間なしフッタ – 株式会社AIコミュニケーション – 総合WEBプロモーション