I'm trying to answer your question.
I suppose that you have an app object and some certificate objects.
At first, you get an App object.
app = App.find(YOUR ID)
app.certificates.each do |c|
# In the loop, you can set your logic
# that selects which certificate is production certificate
# or development certificate
if .....
app.production_certificate = c
elsif .....
app.development_certificate = c
end
end
app.save
I'm not sure the answer can solve your problem. If you have any question, please comment below.