ruby - Friendship Status Detection - Rails 5 -
i'm creating social network ruby on rails , want display following link if users are:
not friends display "not friends" friends display "friends" pending request display "pending"
but i'm getting "uninitialized constant owner::friendship" error on friendship = friendship.where(owner_id: [self.id,owner_2.id], friend_id: [self.id,owner_2.id])
owner.rb
has_many :follows, dependent: :destroy has_many :inverse_follows, class_name: "follow", foreign_key: "friend_id", dependent: :destroy def request_friendship(owner_2) self.friendships.create(friend: owners_2) end def pending_friend_requests_from self.inverse_friendships.where(state: "pending") end def pending_friend_requests_to self.friendships.where(state: "pending") end def active_friends self.friendships.where(state: "active").map(&:friend) + self.inverse_friendships.where(state: "active").map(&:owner) end def friendship_status(owner_2) friendship = friendship.where(owner_id: [self.id,owner_2.id], friend_id: [self.id,owner_2.id]) unless friendship.any? return "not_friends" else if friendship.first.state == "active" return "friends" else if friendship.first.owner == self return "pending" else return "requested" end end end end end
owners_helper.rb
module ownershelper def action_buttons(owner) case current_owner.friendship_status(owner) when "friends" "remove friendship" when "pending" "cancel request" when "requested" "accept or deny" when "not_friends" "add friend" end end end
_owner.html.erb
<h3> <small><%= owner.username %></small><br> <%= owner.fullname %>`</h3> <p> <%= owner.businessname %> </p> <% if current_owner %> <%= action_buttons(owner)%> <% end %>
follow.rb
class follow < applicationrecord belongs_to :owner belongs_to :friend, class_name: "owner" end
friendship_controller.er
class friendshipscontroller < applicationcontroller before_action :authenticate_owner! before_action :set_owner, only: [:create] def create @friendship = current_owner.request_friendship(@owner) respond_to |format| format.html {redirect_to owners_path, notice: "friendship created"} end end def destroy @friendship.destroy respond_to |format| format.html {redirect_to owners_path, notice: "friendship deleted"} end end private def set_owner @owner = owner.find(params[:owner_id]) end end
as mentioned on comments, error happened because there no friendship class. there 2 solutions resolve:
1) change friendship references follow.
2) rename follow class -> friendship
class friendship < activerecord::base self.table_name = "follows" ... end
Comments
Post a Comment