NoMethodError when I try to acces a field of an object taken from the DB

前端 未结 4 1729
[愿得一人]
[愿得一人] 2021-01-25 20:19

  Let\'s say that I have a model called Tweet with the following fields

  • 1. id
  • 2. content
  • 3. created_at
  • 4. user_id
  • <
4条回答
  •  情书的邮戳
    2021-01-25 20:49

    It's because where returns a collection not a single object

    So instead of

    @tweet = Tweet.where(id: 64)
    

    You want

    @tweet = Tweet.find(64)
    

    because you're using the id

提交回复
热议问题