I am writing to my social network I want to develop as an authentication system. I used self-referential association in railwaycasts. I want to solve my small problem, I allow users to view other profiles and use the link to add a friend. But if you are a friend then friends will be shown again. I asked similar questions, but from now on I could not make it.
My Friendship Model:
Class Friendships & lt; ActiveRecord :: Base attr_accessible: friend_id belongs_to: user belongs_to: friend ,: class_name = & gt; "User" Valid: Friend ,: Attendance = & gt; Truth: By the time & gt; : Friend_is_self validates_uniqueness_of: user_id,: scope = & gt; [: Friend_id] def friend_is_self user_id == friend_id? Incorrect: True End Closing My User Model:
.... has_many: friendship is _may: friends ,: through = & gt; : Friendship is _main: inversion_brainnist,: class_name = & gt; "Friendship" ,: foreign_key => "Mitra_ind" is_may: Inversion Sweet ,: By: = & gt; : Inverse_friendships ,: source = & gt; : End of user This is my show.html.erb (user)
& lt; Section & gt; & Lt; H1 & gt; & Lt;% = @ User Username% & gt; & Lt; / H1> & Lt;% Unless existing_user == @ user% & gt; & Lt;% = link_to "ArkadaÅ ?? larÄ ± ma Ekle", friendship (path :: friend_id = & gt; @ user) ;: method = & gt; : Posts, Category: "BTN BTN-Large BTN-Primary"%> & Lt;% end% & gt; & Lt; / Section & gt; I am sorry for such a question, but I can find the correct buddy condition to add a friend. < How about a class method in a friendship? class friendship and lieutenant; ActiveRecord :: Base # ... def self.friendship_exists? (User 1, user2) friendship. Where ("(user_id =? And friend_id =?) Or (user_id =? And friend_id =?)", User1.id, user2id, user2.id, user1.id). Size & gt; 0 End End << Code> Now your can be link_to_if link_to_unless friendship. Friendship_exists? (Current User, @ User) ...
Comments
Post a Comment