mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #9747 from macksmind/refactor_friend_follower
Refactor Person/Friendship relationships to be more intuitive
This commit is contained in:
commit
f5ecb13333
6 changed files with 15 additions and 6 deletions
|
@ -118,7 +118,7 @@ class CounterCacheTest < ActiveRecord::TestCase
|
|||
test "reset the right counter if two have the same foreign key" do
|
||||
michael = people(:michael)
|
||||
assert_nothing_raised(ActiveRecord::StatementInvalid) do
|
||||
Person.reset_counters(michael.id, :followers)
|
||||
Person.reset_counters(michael.id, :friends_too)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
4
activerecord/test/fixtures/friendships.yml
vendored
4
activerecord/test/fixtures/friendships.yml
vendored
|
@ -1,4 +1,4 @@
|
|||
Connection 1:
|
||||
id: 1
|
||||
person_id: 1
|
||||
friend_id: 2
|
||||
friend_id: 1
|
||||
follower_id: 2
|
||||
|
|
3
activerecord/test/fixtures/people.yml
vendored
3
activerecord/test/fixtures/people.yml
vendored
|
@ -5,6 +5,7 @@ michael:
|
|||
number1_fan_id: 3
|
||||
gender: M
|
||||
followers_count: 1
|
||||
friends_too_count: 1
|
||||
david:
|
||||
id: 2
|
||||
first_name: David
|
||||
|
@ -12,6 +13,7 @@ david:
|
|||
number1_fan_id: 1
|
||||
gender: M
|
||||
followers_count: 1
|
||||
friends_too_count: 1
|
||||
susan:
|
||||
id: 3
|
||||
first_name: Susan
|
||||
|
@ -19,3 +21,4 @@ susan:
|
|||
number1_fan_id: 1
|
||||
gender: F
|
||||
followers_count: 1
|
||||
friends_too_count: 1
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class Friendship < ActiveRecord::Base
|
||||
belongs_to :friend, class_name: 'Person'
|
||||
belongs_to :follower, foreign_key: 'friend_id', class_name: 'Person', counter_cache: :followers_count
|
||||
# friend_too exists to test a bug, and probably shouldn't be used elsewhere
|
||||
belongs_to :friend_too, foreign_key: 'friend_id', class_name: 'Person', counter_cache: :friends_too_count
|
||||
belongs_to :follower, class_name: 'Person'
|
||||
end
|
||||
|
|
|
@ -8,7 +8,10 @@ class Person < ActiveRecord::Base
|
|||
has_many :posts_with_no_comments, -> { includes(:comments).where('comments.id is null').references(:comments) },
|
||||
:through => :readers, :source => :post
|
||||
|
||||
has_many :followers, foreign_key: 'friend_id', class_name: 'Friendship'
|
||||
has_many :friendships, foreign_key: 'friend_id'
|
||||
# friends_too exists to test a bug, and probably shouldn't be used elsewhere
|
||||
has_many :friends_too, foreign_key: 'friend_id', class_name: 'Friendship'
|
||||
has_many :followers, through: :friendships
|
||||
|
||||
has_many :references
|
||||
has_many :bad_references
|
||||
|
|
|
@ -280,7 +280,7 @@ ActiveRecord::Schema.define do
|
|||
|
||||
create_table :friendships, :force => true do |t|
|
||||
t.integer :friend_id
|
||||
t.integer :person_id
|
||||
t.integer :follower_id
|
||||
end
|
||||
|
||||
create_table :goofy_string_id, :force => true, :id => false do |t|
|
||||
|
@ -494,6 +494,7 @@ ActiveRecord::Schema.define do
|
|||
t.integer :lock_version, :null => false, :default => 0
|
||||
t.string :comments
|
||||
t.integer :followers_count, :default => 0
|
||||
t.integer :friends_too_count, :default => 0
|
||||
t.references :best_friend
|
||||
t.references :best_friend_of
|
||||
t.integer :insures, null: false, default: 0
|
||||
|
|
Loading…
Reference in a new issue