I am creating an app with a chat function.
The problem is that when I save the rooms table I can't save it due to users validation.
The rooms table and users table have a many-to-many relationship with an intermediate table (room_users) in between.
When saving users, it can be saved without any problem, and the restriction by validation also works as intended. (Force password to mix half-width alphanumeric characters and it works like that.)
I want to save rooms after validating users. How can I solve this? Please professor.
Corresponding source code
Validation failed: Users is an invalid value
class User</pre> <p>■ models/room.rb</p> <pre><code data-language = "Ruby">class Room</pre> <p>■ models/room_user.rb</p> <pre><code data-language = "Ruby">class RoomUser</pre> <strong>What I tried</strong> <p>(1) Comment out the validation of the problem in user.rb → Both rooms and room_users can be saved normally. (Of course, users are not validated)<br /> (2) Change the validation of the problem in user.rb to a unique constraint → Both rooms and room_users are not saved due to the same error.<br /> ③ After applying validation, save users → save. Validation is intended to specify password as a mixture of alphanumeric characters, but it works as it is.<br /> ④ Try to prepare binding.pry with the create action of rooms. → params is sending the parameters as intended.</p> <pre><code>From: /Users/kintarou/projects/good-question/app/controllers/rooms_controller.rb: 9 RoomsController # create: 7: def create 8: @room = Room.new (room_params) =>9: binding.pry 10: if @ room.save 11: redirect_to root_path 12: else 13: render: new 14: end 15: end  pry (#<RoomsController>)>@room =>#<Room: 0x00007f843bdd0330 id: nil, room_name: "Next meeting appointment", created_at: nil, updated_at: nil> pry (#<RoomsController>)>Started GET "/ cable" for :: 1 at 2020-11-10 14:42:27 +0900  pry (#<RoomsController>)>params =>"lqTqiP2G7lc/kKrguWt7DrNNPPYgsAcX4ISbx/kvFe51zWC + PUvWDr6TOdDTn9HHphM1zm1eAQxrvS5gHKXaKQ ==", " ,, "room_name" =>"Schedule for next meeting"} permitted: false>, "commit" =>"Create room", "controller" =>"rooms", "action" =>"create"} permitted: false> pry (#<RoomsController>)>@ room.save! ActiveRecord :: RecordInvalid: Validation failed: Users is an invalid value from /Users/kintarou/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-18.104.22.168/lib/active_record/validations.rb:80:in `raise_validation_error'  pry (#<RoomsController>)>
⑤ Server restart/PC restart → The situation does not change.Supplementary information (FW/tool version, etc.)
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
Please forgive me for the fact that I may not be able to get the point with the first question and there may be missing information.
I would appreciate it if you could teach at the same time.
I look forward to working with you.
Answer # 1
I'm not sure, so it's just a little research.
If you're having trouble with validation
belongs_to: user, validate: falseI felt that I could avoid it. (API dock: belongs_to)
When set to true, validates new objects added to association when saving the parent object. False by default. If you want to ensure associated objects are revalidated on every update, use validates_associated.
defaultSo. .. .. I wonder what about
Answer # 2
bin/rails consoleCan I save it after launching and running the following code?
user_ids = [1, 2] room = Room.new (room_name: "Next meeting appointment") room.users<<User.where (id: user_ids) room.save!
If you can save it with this, rewriting it like this may work.
If the above code doesn't work, forget about it because it's wrong.
def create user_ids = params [: room] [: user_ids] # =>["1", "2"] @room = Room.new (room_params) @ room.users<<User.where (id: user_ids) if @ room.save redirect_to root_path else else render: new end end
- ruby on rails - image cannot be saved (activestorage installed) null in image of table
- ruby - account id cannot be saved with stripe
- ruby - validation cannot be granted
- ruby on rails - the image cannot be saved in s3 and cannot be displayed in the view aws :: sigv4 :: errors :: missingcredentials
- ruby on rails - data cannot be saved in the intermediate table (parameters cannot be passed)
- ruby on rails - date_select cannot be saved
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby on rails 5 - the time notation cannot be written normally
- ruby bcrypt cannot be installed
- ruby on rails 5 - cannot check the existence of a child model that has a parent-child relationship
- sql execution result cannot be received and displayed in ruby
- ruby - [rails] cannot refer to the relation destination table
- ruby - activerecord :: recordnotfound shows that the id cannot be found
- ruby - validation does not start
- ruby - implementation of validation (when blank transmission)
- ruby - gtk3 cannot be installed
- ruby - [rails] when rails s is executed, an error "cannot load database configuration" appears and it is forcibly term
- ruby on rails runtime error cannot be resolved
- ruby - asynchronous communication of like function cannot be implemented
- ruby - tag posts uninitialized constant post :: tagrelationship
- ruby - i get an error saying undefined method `resize_to_limit'for
- ruby - i want to save it in two tables when posting with form_with
- ruby - passing variables using render partial
- ruby - i get an error when testing rspec instance variables
- ruby - how to implement the edit/update function of multiple tables when using form object in rails
- ruby - all rails gems are not installed
- ruby - output the total integer value held by each user
- ruby - i want to use the search method for active hash data