There are User model and Message model, and they are related as follows.
# user.rb has_many: send_messages, class_name: "Message", foreign_key: "send_user_id", dependent:: destroy has_many: recive_messages, class_name: "Message", foreign_key: "recive_user_id", dependent:: destroy;
# message.rb belongs_to: send_user, class_name: "User", foreign_key: "send_user_id", optional: true belongs_to: recive_user, class_name: "User", foreign_key: "recive_user_id", optional: true
I also confirmed that it can be associated and called firmly on the console in this way.
However, it cannot be called with the following code.
# users.controller def show @user = User.find_by (id: params [: id]) @recive_messages = @ user.recive_messages.order (id: "DESC") @send_messages = @user .send_messages.order (id: "DESC") end
# users/show.html.erb <% @ recive_messages.each do | message |%> #Image upload uses carrierwave <% = message.send_user.profile_image.url%> <% = message.send_user.username%> <% end%>
Both can be called on the console, but in view I get a NomethodError.
Thanks for your cooperation.
Answer # 1
It seems that an error occurred when there was no user belonging to message without logging in because it was possible to post even when message was not logged in.
I was able to solve it if I made a conditional branch firmly
- ruby on rails - rails associated model is not saved in db
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby on rails - i want to validate only the foreign key on the model side
- ruby - the last db setting doesn't work when deploying rails app on aws
- ruby on rails - i want to implement a function for administrator users to register luggage for general users in rails
- ruby on rails 6 - environment construction of ruby on rails
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby - when deleting a post, i want to delete the comment associated with the model together
- ruby on rails - error in controller when transitioning with rails6 destory action
- ruby on rails - access fails after seed after deploying to heroku
- ruby on rails runtime error cannot be resolved
- ruby on rails - rails s can't
- ruby on rails - missing required keys: i'm having trouble resolving the [: id] error
- ruby on rails 5 - some items are not saved in db when posting from rails form
- bootstrap display in ruby on rails is broken
- ruby - [rails] i can't install the add favorite button
- ruby on rails - dealing with wrong number of arguments (given 1, expected 2)
- ruby on rails - seed fails on heroku
- ruby on rails - cannot pass id to destroy action of intermediate table
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs