ruby on rails - Polymorphic Association On UUID and Integer Fields -


given tables integer , uuid primary keys best way integrate polymorphic join (has_many)? example:

class interest < activerecord::base   # id integer   has_many :likes, as: :likeable end  class post < activerecord::base   # id uuid   has_many :likes, as: :likeable end  class user < activerecord::base   has_many :likes   has_many :posts, through: :likes, source: :likeable, source_type: "post"   has_many :interests, through: :likes, source: :likeable, source_type: "interest" end  class < activerecord::base   # likeable_id , likeable_type strings   belongs_to :likeable, polymorphic: true   belongs_to :user end 

many queries work:

interest.likes post.likes user.likes 

however:

user.interests 

gives:

pg::undefinedfunction: error: operator not exist: integer = character varying line 1: ...interests" inner join "likes" on "interests"."id" = "likes".... ^ hint: no operator matches given name , argument type(s). might need add explicit type casts. : select "interests".* "interests" inner join "likes" on "interests"."id" = "likes"."likeable_id" "likes"."user_id" = $1 , "likes"."likeable_type" = $2

what's best way include ensure proper casting happens?

i'm not activerecord, , not answer you're looking for, if need temporary *ugly workaround till can find solution, override getter :

class user   def interests     self.likes.select{|like| like.likeable._type == 'interest'}.map(&:likeable)   end end 

*very ugly cause load user likes , sort them

edit found this interesting article :

self.likes.inject([]) |result, like|   result << like.likeable if like.likeable._type = 'interest'   result end 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -