###前提・実現したいこと
質問失礼します。
ただいまrails tutorialの2章を進めているのですが、
データベースのユーザーとマイクロポストの関連付けができない状態になっています。
「rails console」のコマンドを実行したところ以下のようになりました。
###発生している問題・エラーメッセージ
$ rails console
User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, name: "tamura shohe", email: "da", created_at: "2017-06-07 07:51:09", updated_at: "2017-06-07 07:51:09">
2.3.0 :002 > first_user.microposts
NameError: undefined local variable or method `first_user' for main:Object
from (irb):2
###該当のソースコード
こちらにファイル状況など記載いたします。
「app/models/micropost.rb」
class Micropost < ApplicationRecord
belong_to:user
validates :content, length: { maximum: 140 }
end
「app/models/user.rb」
class User < ApplicationRecord
has_many :microposts
end
「config/routes.rb」
Rails.application.routes.draw do
resources :microposts
resources :users
root 'users#id'
end
「db/migrate」(Users)
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps end
end
end
「db/migrate」(Microposts)
class CreateMicroposts < ActiveRecord::Migration[5.1]
def change
create_table :microposts do |t|
t.text :content
t.integer :user_id
t.timestamps end
end
end
###試したこと
マイクロポスト側に問題がありそうです。ちなみにマイクロポストの文字数を140文字にするために「app/models/micropost.rb」にて
class Micropost < ApplicationRecord
validates :content, length: { maximum: 140 }
end
という記述をしたところ、設定が反映されませんでした。
何卒、お力添えよろしくお願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
RUby : 2.3.0
rails : 5.1.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/08 05:30