When I faced this problem, I spent 3~4 hours solving it. I’ll summarize it in order not to forget it again.

User.transaction do   
user = User.create!(param)
UserMailWorker.perform_async(user.id)
end

It is a pity that this code sometimes doesn’t work because UserMailWorker is executed before transaction is committed. Thus, there is no user when Worker is executed. This issue is mentioned in Sidekiq.

It is very simple.

User.transaction do   
user = User.create!(param)
end
UserMailWorker.perform_async(user.id)

This code will work rightly!


I have been developing software which enable my colleagues to finish their tasks more effectively. In order to make it, it is necessary to use a library, fabric js. Since there was a problem it took much time to solve, I’ ll introduce it.

Library => http://fabricjs.com/

There is texts on a rectangle in canvas like below.


I used sidekiq-cron gem below for the first time. It was so handy I thought I would use it many times in the future. I’ll summarize how to use it in order not to forget it.

This gem is a scheduling application for sidekiq. It is possible to execute scheduled functions such as sending an email at 9:00 am every day.

# sidekiq.rbSidekiq.configure_server do |config|
config.redis = redis_config
unless Rails.env.test?
schedule_file = 'config/schedule.yml'
if File.exist?(schedule_file) && Sidekiq.server?
Sidekiq::Cron::Job.load_from_hash(YAML.load_file(schedule_file))
end
end
end

We can let sidekiq-cron read files in which functions are written.

# schedule.ymlfunction_name:
cron: "*/10 *…

There is useEffect hook in react replacing componentDidMount and componentDidUpdate with itself. We always have to pay attention to this matter.

If useEffect is executed or not is decided by if a state is changed or not. For example,

const [checked, setChecked] = useState(false)useEffect(() => {  console.log('executed!')
}, [checked])

when “checked state” changes, useEffect is executed.

However, suppose useEffect refers to an array or an object, what would happen?

function App() {  const colors = ['red', 'blue', 'green']
useEffect(() => {
console.log('executed!')
}, [colors])
return <div>component</div>}

useEffect always refers to new array when the component is rendered. As…


We have to be careful not to forget it.

When I was writing codes like below referring to a book, I noticed the fact that each method is not used but included.

class Comics  extend Forwardable  def_delegators :@comics, :size, :each  include Enumerable
def initialize(comics) @comics = comics end def publishings @comics.select{ |comic| comic.published } endend

If each is not included, the codes output an error like this in publishing method.

undefined method `each' for #<Comics:0x00007fde00873030> (NoMethodError)

That was confusing because each method was not defined in Comics class.

However, checking out ruby-doc helped me to understand the cause.

“The…


This is a famous trick in object oriented programming, so I thought that it needed writing down. The codes are quoted from “practical object-oriented design in ruby”.

class Trip  attr_reader :bicycles, :customers, :vehicle  def prepare(prepares)    prepares.each { |preparer|      case preparer      when Mechanic        preparer.prepare_bicycles(bicycles)      when TripCoordinator        preparer.buy_food(customers)      when Driver        preparer.gas_up(vehicle)        preparer.fill_water_tank(vehicle)      end     }  endendclass TripCoordinator  def buy_food(customers)  endendclass Driver  def gas_up(vehicle)  end
def fill_water_tank(vehicle) endend

If your code distinguish which method should be called with class name, it may be the duck. It is possible to make the codes easier with duck typing.

class Trip

I spent four days solving a problem. Of course, I lacked of knowledge about authorizations, but this was very tricky, so I’m going to write down the problem and the solution.

I was developing an application using Oauth authorization. The libraries were omniauth-oauth2 and doorkeeper.

I managed to finish it up on the development environment, and I merged it to the staging environment. However, it didn’t work well. The logger showed me the error “invalid credential”. When I saw the error, I first thought about the possibility that my codes were wrong , but if my codes were wrong, the…


I knew that the rule existed but I couldn’t imagine it properly. I wasn’t able to understand “inversion” with codes. That’s why I actually wrote codes about DIP in order to make easier to comprehend it. The codes are quoted from “practical object-oriented design in ruby”.

# wheel class depends on gear classclass Gear
attr_reader :chainring, :cog
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def gear_inches(diameter)
ratio * diameter
end
def ratio
chainring / cog.to_f
end
end
class Wheel
attr_reader :rim, :tire, :gear
def initialize(rim, tire, chainring, cog)
@rim = rim…

There might be a time to change and override the view of Doorkeeper.

Of course, the way is mentioned in wiki of Doorkeeper.

# config/application.rbconfig.to_prepare do
# Only Applications list
Doorkeeper::ApplicationsController.layout "my_layout"
# Only Authorization endpoint
Doorkeeper::AuthorizationsController.layout "my_layout"
# Only Authorized Applications
Doorkeeper::AuthorizedApplicationsController.layout "my_layout"
end

However, I couldn‘t understand ‘my_layout’. In this case, ‘my_layout’ means the file name of a html file you want to adapt. For example, if you have a file ‘application.html.erb’, it is possible to write code like below.

# config/application.rbconfig.to_prepare do
# Only Applications list
Doorkeeper::ApplicationsController.layout "application"
end

As I told you, we have…


problem

This article will introduce how to get a response in a system test, which means I want to check out whether the chrome driver(I use this driver) automatically kick a correct request. In webmock docs, the explanation is like below.

stub_request(:get, 'www.example.com').
with(headers: {'Accept' => ['image/jpeg', 'image/png'] })

req = Net::HTTP::Get.new("/")
req['Accept'] = ['image/png']
req.add_field('Accept', 'image/jpeg')
Net::HTTP.start("www.example.com") {|http| http.request(req) } # ===> Success

However, this is not useful in a system test.(I might lack learning…) because the test above kick a request api in a test itself. I wrote the code that calls api in an application. …

Tomoharu Tsutsumi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store