Rex don't attack mole rats because of the perk. Remember the King telling you about it, how he hates mole and giant rats?
And man, this is so true, I was next to the Legion Raid Camp and suddenly Rex starts to growl and runned like the devil.
When I noted, he was killing the mole rats nearby and chase one all the way until the Broc Flower Cave entrence.
I laughed so much.
The Animal Friend perk does not apply to companions, however they are still indirectly affected by it.
There are three factions involved here:
Companion
Player
Animal Friend
By default, the player character is a member of the Player faction, all companions are members of the Companion faction, and a list of various critters comprises the membership of the Animal Friend faction. Furthermore, the Companion faction is, by default, allied with the Player faction, and as a result shares all affiliations the Player faction possesses. Taking the first rank of the Animal Friend perk adds the player to the Animal Friend faction as a friend, thus by extension the Companion faction is treated as being friendly too.
Taking the second rank, by the way, promotes you to 'ally' status with the AF faction, which is why they come to your aid during a fight.