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
Related articles
- ruby - i don't know where to write class in select tag
- ruby - △△ how to write haml
- ios - where is the file to write gitignore?
- ruby - where method doesn't work
- ruby on rails 6 - can i write font awesome in rails6 helper?
- ruby on rails - how to write in haml?
- ruby on rails - how to write a url helper
- ruby on rails - the request spec of the show page link where the id randomly enters does not pass
- how to write in ruby multiple loop controller
- ruby data acquisition how to write ruby
- ruby on rails - how to write configuration file when you want to use single file component with rails, vuejs
- ruby on rails - where is capistrano's "bundle exec cap production deploy" executed?
- i'm confused about how to write ruby on rails
- ruby - bundle install does not have write permission and error occurs
- ruby tell me where the end is missing
- ruby - i want to write a little smarter (returns the maximum index of the array)
- ruby - rails json i don't know how to write json conditional branch that is returned when selected
- ruby on rails - i want to write a test code that matches the rspec value
- ruby on rails - how to write a form object
- ruby - i want to write a description for saving data in the create action and save it in the db (prototypes table)
Related questions
- in ruby on rails, i want to limit it to information other than foreign key constraint in references
- ruby - [rails] duplicate column name: it is displayed as reset_sent_at, but i do not know the duplicated part
- ruby - i don't feel that the sql issued by rails has a different format from the sql that i usually write
- ruby - i want to solve the n + 1 problem when using rails where method in loop processing
- ruby - i want to get it at intervals by searching using rails active record
- ruby - the image is displayed as strange characters in rails
- html - i would like to know how to calculate the average of the data
- ruby - implementation of friendly forwarding
- ruby - i installed carrierwave with rails, but i can't see the image
- ruby - i get an error with the rails new command
If it's about 1 line, you don't have to bother with the model. You can write the controller directly.