Rails 4.2.6を使用して、アプリケーションを構築中です。
以下のようなコードを書きました。
user = User.create([ email: 'hoge@example.com' ]) user_profile = UserProfile.new( name: 'hoge taro' ) user.user_profile << user_profile
class UserProfile < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_one :user_profile, :foreign_key => 'id' end
テーブル
create_table "user_profiles", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end
期待する動き
user.user_profile << user_profile
上記を実行した時点で、user_profilesにnameが保存される
現状
undefined method `user_profile' for #Array:0x0000000fc865b8
上記がエラーとして出現する。
参考にしたサイト
Railsで関連するデータ(親子関係)を保存する方法あれこれ
お分かりになる方、ご教授頂ければ幸いです。
よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/12 08:26 編集
2017/10/12 08:42
2017/10/12 09:03