NoMethodError - undefined method 'find_by' for []:ActiveRecord::Relation
Asked Answered
O

2

9

I've been following Michael Heartl tutorial to create a follow system but I have a strange error: "undefined method `find_by' for []:ActiveRecord::Relation". I'm using devise for authentication.

My view /users/show.html.erb looks like that:

.
.
.
<% if current_user.following?(@user) %>
    <%= render 'unfollow' %>
<% else %>
    <%= render 'follow' %>
<% end %>

User model 'models/user.rb' :

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,     :trackable, :validatable

has_many :authentications
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower

    def following?(other_user)
        relationships.find_by(followed_id: other_user.id)
    end

    def follow!(other_user)
        relationships.create!(followed_id: other_user.id)
    end

    def unfollow!(other_user)
        relationships.find_by(followed_id: other_user.id).destroy
    end

end

Relationship model 'models/relationship.rb':

class Relationship < ActiveRecord::Base

  attr_accessible :followed_id, :follower_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true

end

Rails is telling me that the issue is in user model : "relationships.find_by(followed_id: other_user.id)" because mthod is not defined, but I don't understand why ?

Oviform answered 19/7, 2013 at 15:54 Comment(0)
I
26

I believe find_by was introduced in rails 4. If you are not using rails 4, replace find_by by a combination of where and first.

relationships.where(followed_id: other_user.id).first

You can also use the dynamic find_by_attribute

relationships.find_by_followed_id(other_user.id)

ASIDE:

I suggest you change your following? method to return a truthy value rather than a record (or nil when no record is found). You can do this by using exists?.

relationships.where(followed_id: other_user.id).exists?

One big advantage of this is it doesn't create any object and just returns a boolean value.

Impeach answered 19/7, 2013 at 16:8 Comment(1)
Working, thanks ! And you're right for boolean value, it's much better.Oviform
J
3

You can use

relationships.find_by_followed_id( other_user_id ) 

or

relationships.find_all_by_followed_id( other_user_id ).first
Jamal answered 3/10, 2013 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.