Ich habe Michael Heartl Tutorial folgen, um ein Follow-System zu erstellen, aber ich habe einen seltsamen Fehler: "undefined Methode` find_by 'für []: ActiveRecord: :Beziehung". Ich verwende ein Gerät zur Authentifizierung.NoMethodError - undefined Methode 'find_by' für []: ActiveRecord :: Relation
Meine Ansicht /users/show.html.erb sieht wie folgt aus:
.
.
.
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
User-Modell '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-Modell ‚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 sagt mir, dass das Problem in Benutzermodell ist: "relationships.find_by (followed_id: other_user.id)", weil m thod ist nicht definiert, aber ich verstehe nicht warum?
Arbeiten nutzen, danke! Und du hast Recht für booleschen Wert, es ist viel besser. – titibouboul