accepts_nested_attributes_for with belongs_to polymorphic

后端 未结 4 676
渐次进展
渐次进展 2020-11-27 10:37

I would like set up a polymorphic relation with accepts_nested_attributes_for. Here is the code:

class Contact 

        
4条回答
  •  情歌与酒
    2020-11-27 11:17

    The above answer is great but not working with the setup shown. It inspired me and i was able to create a working solution:

    works for creating and updating

    class Job :true
      attr_accessible :client_attributes
      accepts_nested_attributes_for :client
    
      def attributes=(attributes = {})
        self.client_type = attributes[:client_type]
        super
      end
    
      def client_attributes=(attributes)
        some_client = self.client_type.constantize.find_or_initilize_by_id(self.client_id)
        some_client.attributes = attributes
        self.client = some_client
      end
    end
    

提交回复
热议问题