Rohit Kumar
Rohit Kumar

Reputation: 85

change type in polymorphic association in rails

I am new to rails and I'm using rails Polymorphic association and I ran into a problem I have two models EmployeeTable and ProductTable. Both have some picture in Picture Model

class Picture < ActiveRecord::Base
   belongs_to :imageable, polymorphic: true
end

class EmployeeTable < ActiveRecord::Base
   has_many :pictures, as: :imageable
end

class ProductTable < ActiveRecord::Base
  has_many :pictures, as: :imageable
end

there are two column in Picture imagable_id and imagable_type. When I create an entry in table it stores imagable_type as either "EmployeeTable" or "ProductTable". Can I change these name to any different Names like "employee" and "product".

Thanks in advance

Upvotes: 1

Views: 1497

Answers (1)

Athar
Athar

Reputation: 3268

To retreive the image_type without with table, you can add this method in your model. Changing the assosiated model name while saving will cause issues while querying

def neat_imageable_type
   self.imageable_type.gsub("Table", "").downcase
end

Upvotes: 1

Related Questions