railsにおけるデータベース間のリレーション
railsでデータベースのリレーションはMODEL.rbファイルに記述する. 1対1の場合所有しているモデルにhas_one,所属しているモデルにbelongs_toを設定する.設定するときは最初の一文字を小文字にする.
リレーションの設定が完了したらコントローラ内でデータを呼び出すことができる.
例) Userのカラム
id, name
Profileのカラム
id, user_id, description
Userのリレーション
class User < ActiveRecords :: Base has_one :profile # profileを所有している end
Profileのリレーション
class Profile < ActiveRecords :: Base belongs_to :user # userに所属している. end