[Rails] Store only checked items in DB

After asking a question on teratail, I solved it myself. The site is so heavy (?) I couldn't post it, so I'll write it here first.

What I wanted to do

I was making a movie app and needed to store some simple information about the movie in a database. Since it was dull to input from the terminal, I will not publish it in ver.1 and will improve it for users later, so I created a posting form in the application. The following movie information can be registered in the database with the new method.

--title: Title of the work --directed: Director's name --story: Synopsis --service: Delivery service name from which the synopsis is quoted --time: Playback time

I wanted to select the delivery service name from these from the four check boxes and store the checked service name in the database, but the article I referred to was the `check box. Since the content was to store the item and check status in an array, it was necessary to "extract only the checked value" in order to realize it.

Checkboxes are placed using the check_box tag.

<div class="box form4">
  <%= check_box "service", "PrimeVideo" %>PrimeVideo
  <%= check_box "service", "Netflix" %>Netflix
  <%= check_box "service", "Hulu" %>Hulu
  <%= check_box "service", "U-NEXT" %>U-NEXT
</div>

Directly in the controller

service: params[:service]

If you write, the generated hash will be stored in the service column of the DB. If unchecked, the value will be 0, and if it is, 1 will be the value.

{"PrimeVideo"=>"0", "Netflix"=>"1", "Hulu"=>"0", "U-NEXT"=>"0"}

From here, I want to extract and save only the service name of the checked item (that is, the key whose value is 1)! Perhaps because I didn't have enough basic knowledge in the first place, I didn't know how to do it and left it alone.

Solution###

def req
 params[:service].each do | di1 , di2 |
  if di2 == "1"
   @movie = Movie.new(
	  title: params[:title],
	  directed: params[:directed],
	  story: params[:story],
	  service:di1,
	  time: params[:time]
   )
   @movie.save
  end
 end
end

I used the code of Article that I originally referred to.

--Receive the value with params [: service] and store the service name andcheck state (0 or 1)in each of the two variables. --If the check status is 1, save the service name stored in the companion variable in the service column.

Follow the procedure.

If the service name is not checked, ** save method will not be executed in the first place **, but ... I don't have time, so now I just want to move forward ... When I'm calm, I'll try to lay the groundwork focusing on the parts that can be made into a little more polite code, such as the select method, the keys method, and the SQL syntax.

We would like to express our sincere gratitude to all the respondents who gave us the opportunity.

Recommended Posts

[Rails] Store only checked items in DB
rails test db only drop
Error in rails db: migrate
Group_by in Rails
[Japanese localization] i18n rails Easy Japanese localization view display only
rails db: 〇〇 Summary
Simple notification function in Rails (only when followed)
[Ruby On Rails] How to reset DB in Heroku
twitter4j java Store the searched tweets in DB (MySQL).
Model association in Rails
Adding columns in Rails
Disable turbolinks in Rails
CSRF measures in Rails
rails db: migrate failed!
[Rails] rails db command summary
Use images in Rails
Understand migration in rails
Split routes.rb in Rails6
Implement markdown in Rails
[Rails] Introducing Travis CI and getting stuck in db: create