У меня есть этот тест из книги Майкла Хартла
describe "follower/following counts" do
let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }
before do
sign_in(user)
visit root_path
user.follow!(other_user)
end
it { should have_link('0 following', href: following_user_path(user)) }
it { should have_link('1 follower', href: followers_user_path(user)) }
end
проблема в том, что это всегда не удается
it { should have_link('1 follower', href: followers_user_path(user)) }
но когда я изменяю его на
it { should have_link('0 follower', href: followers_user_path(user)) }
тест проходит.Вот мой код модели:
User.rb
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :followers, through: :reverse_relationships, source: :follower
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
Relationship.rb
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
Routes.rb
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]