I have 3 tables, cooler,cpu,socket. A cooler (supports/)has_many sockets and cpu belongs_to socket. Now I what is the easiest way to check if cooler supports cpu or not?
@cooler.sockets.include? @cpu.socket # => False always no matter what
I tried to check types using:
@cooler.sockets.class
#=> #Socket::ActiveRecord_Associations_CollectionProxy
Your code:
always returns
false, because here you are trying to check ifCpu's model instancesocketattribute is contained in the array ofSocketmodel instances corresponding to theCoolermodel instance, and that can never possibly happen.However, you can accomplish what you want with
.exists?:or
.present?: