Home>

For example, suppose there are two related models (tables) as follows
User, Book
User and Book are related one-to-many

class User</pre>
<p><br />
As a model class is generated.<br />
For example if I want to display users in the/users/index action and in the view</p>
<pre><code>User.includes (: books) .where (id: user_id)
class User</pre>
Which should be written in<p>?</p>
<p>Since I want to show a list of users, I wonder if I should write in the User model in light of the principle of single responsibility.</p>
<p>If I want to display a list of statuses of users who are linked to User but unrelated to Book,</p>
<pre><code>Status.includes (: users) .where (users: {id: user_id})


If i create a method like this, you don't know if you should write it in the Status model or write a method in the User model. (To make User a receiver of includes is in the direction of nothing.)

If i write this in Status, if I want to display status and users in/users/index,

User.new
Status.new


I have to create two model class instances and I feel redundant.

I'm worried about class design. How can I solve it?

  • Answer # 1

      

    Which should I write?

    If it's about 1 line, you don't have to bother with the model. You can write the controller directly.