[Ruby] [Past] Part of the answer memo when mentoring TECH::CAMP

2 minute read

Past POST

I will release the contents I wrote down when I was a mentor of TECH::CAMP in the past. I tried to divide it into small parts, but I will put together small ones. Since my memory is ambiguous, there may be some mistakes…

Q. I can’t retrieve the contents of the hash.

movie = {“title” => “Harry Potter”, “genre” => “fantasy”, “year” => “2001”} Let’s write the code that takes the string you want to take from. I think it was a problem like this.

Applicable code


def movie_info(movie, data)
  puts movie[data]
end

movie = {"title" => "Harry Potter", "genre" => "fantasy", "year" => "2001"}

"puts" Please select one from the following and input.
  Title
  Genre
  Year"

info = gets.chomp

movie_info(movie, info)

Explanation

here puts movie[:data] It will not work if you try. The reason is that the type is different.

info = gets.chomp Since the key is received here as a “character string”, The hash definition also uses the string “title” so there is no problem here.

puts movie[:data] On the other hand, if you write like this, it will output in symbol type, Nothing is output because the type is different.

By the way, you can check the type by using .class. ex) info.class

If you want to use the symbol type movie = {title: => "Harry Potter"} Change the definition of hash like info = gets.chomp.to_sym If you rewrite like this, it will work.

puts movie[data.to_sym] But it’s okay. (Converted to symbol type)

Q.devise, no method error

When there is no method error related to devise. There is no current_sign_in_at, etc.

Workaround

Check the corresponding migration file created by devise Uncomment around the relevant part Recreate the database $ rake db:migrate:reset You can do this all at once with this command.

This command drops the database once, This is a command to recreate the database based on the existing migration file. Of course, the data in the database will be erased. If you have various data and want to keep it, I think you should output the data in CSV and save it somewhere before doing it.

If you have just migrated, you may want to rollback to go back.

$ rake db:migrate:status

You can check which files are currently migrated with.

*By the way, it doesn’t make sense to do db:reset. It’s just recreated from the schema file.

Cause

When installing device, leave the commented out items by default. The method you want to use was not generated. It should be resolved by redoing the migration again.

On Q.heroku, id is added in increments of 10

There is no problem in the development environment, but it changes only in the production environment. I don’t think many people care about it, but this is the default setting for clearDB. Heroku seems to use cleardb as a db server.

【reference】 http://w2.cleardb.net/faqs/#general_16 When I use auto_increment keys (or sequences) in my database, they increment by 10 with varying offsets.Why? ClearDB uses circular replication to provide master-master MySQL support.As such, certain things such as auto_increment keys (or sequences) must be configured in order for one master not to use the same key as the other, in all cases.We do this by configuring MySQL to skip certain keys, and by enforcing MySQL to use a specific offset for each key used.The reason why we use a value of 10 instead of 2 is for future development.