Remove Association Programmatically in Rails

I am writing an extension for Spree and I want to delete an existing association.

Given the following code:

class Project < ActiveRecord::Base
  has_one :status
  ...
end

How to delete a call has_one :statusat runtime? I want to remove the association and its associated methods.

+3
source share
2 answers

Unfortunately, a rather complicated DSL call that adds a lot of methods to the class, you have to remove all of them, and probably it would not be worth it.

It might be easier to create a new CleanProject class, add a Project object to it using composition or inheritance, and then just go through the calls for those parts of the project that you want.

, , , ( , , ), :

status.project_id = nil
status.save
0

, ?

, , , cronjob ? , .

def need_assoc
  eval <<-EOC
    class Project < ActiveRecord::Base
    has_one :status
    ...
    end
  EOC
end

eval .

-1

Source: https://habr.com/ru/post/1726313/


All Articles