Home>

Recently, the "world's worst login code" has become a hot topic, but it is not a security issue to place data in Rails and use for or each in the view. What?

Now, we have created the Users table and the Musics table that stores the songs associated with each user, and the process of retrieving them in the view is written as follows. (In Haml)

[email protected] musics.each do | music |
          -user = User.find_by (id: musics.user_id)
          % tr
            % td = music.user_id
            % td = user.username
            % td = music.title


On the controller side, only the process of putting Musics.all into @musics.

Is it better to perform all processing on the controller side except for displaying data?
If it is processed on the controller side, I can not think of a method, but what kind of method is there ...

Please teach me. .

  • Answer # 1

    If you declarebelongs_to: userto

    Music, you can get users linked bymusic.user.

    If it is left as it is,musicwill be subtracted byuser, so@musics = Music.include (: user) .where .. Let's pull it together like..

    Active Record Association (Association)-Rails Guide