Accessing a has_one associations' attributes

后端 未结 4 545
暗喜
暗喜 2021-01-06 00:26

I\'m still quite new to Rails so hopefully this isn\'t a silly question.

I have two models: User and Chore. User has_one chore, and Chore belongs to User

<         


        
4条回答
  •  余生分开走
    2021-01-06 00:49

    Another option:

    <%= user.chore.try(:name) %>
    

    This will return nil if there is no user.chore.

提交回复
热议问题