lundi 28 décembre 2015

How to access user table data based on friends table

I am new for ruby on rails, i am trying to access User Details based on the User_id in the Friends Table.

ChatController:

class ChatController < ApplicationController
    def dashboard
        @avatar = Profilephoto.find_all_by_user_id(session[:userid])
        frnds_list_with_status_and_msg_count
    end

    def frnds_list_with_status_and_msg_count
        @id = session[:userid]
        #getting list of friends having for the current user
        @frnds = Friend.find_all_by_user_id(@id)
    end

    def getmessage      
    end     
end

models/User.rb

class User < ActiveRecord::Base
    has_and_belongs_to_many :friends 
end

models/friend.rb

class Friend < ActiveRecord::Base
    belongs_to :users
end

Here the problem i am facing is:

I am trying to access user friends from friends table and details from user table.

users table(id, name, paswd, nickname, fname, lname, status)

frieds table(id, user_id, friend_id)

Aucun commentaire:

Enregistrer un commentaire