Question

I have a Rails 4 app which has a Devise user. The Devise user can create multiple Apps and each App can contain multiple Certificates. At any given time tho and App can only have one production_certificate and one development_certificate. I believe that i setup the associations correctly here they are. My trouble now is how can i set the App to use one out of the many Certificates that an app owns to be the production/development certificate.

class App < ActiveRecord::Base
  belongs_to :user

  has_many :certificates

  belongs_to :production_certificate, class_name: 'Certificate'
  belongs_to :development_certificate, class_name: 'Certificate'
end

class Certificate < ActiveRecord::Base
  belongs_to :app
end
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top