Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create_table "animal_friends", :force => true do |t|
- t.integer "animal_id"
- t.integer "animal_friend_id"
- t.datetime "created_at"
- t.datetime "updated_at"
- t.integer "status_id", :default => 1
- end
- SELECT animals.*
- from animals join animal_friends as af
- on animals.id =
- case when af.animal_id = #{id} then af.animal_friend_id else af.animal_id end
- WHERE #{id} in (af.animal_id, af.animal_friend_id)
- has_many :friends, :class_name => "Animal", :finder_sql => 'SELECT animals.* from animals join animal_friends as af on animals.id = case when af.animal_id = #{id} then af.animal_friend_id else af.animal_id end ' +
- 'WHERE #{id} in (af.animal_id, af.animal_friend_id) and status_id = #{Status::CONFIRMED.id}'
- @animal.friends.first
- CREATE VIEW with_inverse_animal_friends (
- SELECT id,
- animal_id,
- animal_friend_id,
- created_at,
- updated_at,
- status_id
- FROM animal_friends
- UNION
- SELECT id,
- animal_friend_id AS animal_id,
- animal_id AS animal_friend_id,
- created_at,
- updated_at,
- status_id
- FROM animal_friends
- )
- CREATE VIEW unique_animal_friends (
- SELECT MIN(id), animal_id, animal_friend_id, MIN(created_at), MAX(updated_at), MIN(status_id)
- FROM
- (SELECT id,
- animal_id,
- animal_friend_id,
- created_at,
- updated_at,
- status_id
- FROM animal_friends
- UNION
- SELECT id,
- animal_friend_id AS animal_id,
- animal_id AS animal_friend_id,
- created_at,
- updated_at,
- status_id
- FROM animal_friends) AS all_animal_friends
- GROUP BY animal_id, animal_friend_id
- )
- class Animal < ActiveRecord::Base
- has_many :unique_animal_friends
- has_many :friends, :through => :unique_animal_friends
- end
- class UniqueAnimalFriend < ActiveRecord::Base
- belongs_to :animal
- belongs_to :friend, :class_name => "Animal"
- end
Add Comment
Please, Sign In to add comment