Topic: how to find records from another table in rails?

I have welcome,user,college,education model..
On the welcome page i want to show messages posted by only those users who have gone to the same college as the current user...but i am not able to show that. i have written following code in my welcome controller :

    def index
      @welcome = Welcome.all
      @newmessage = Welcome.new
      @college = User.find(@current_user.id).colleges
    end

using first line i am able to show messages posted by all users inside my view as follows

%section.container_6.clearfix

  .grid_6   

    = div_for welcome do

      %tr

        %ul{:style=>"list-style-type:square"}

          %td

          %li= welcome.message

          %td       

          Posted

          %span= time_ago_in_words(welcome.created_at) 

          ago

          %br

          %br   

             
but that is not what i want.Using third line i am able to find current users college data but i am not sure how can i use this data...

how can i search only those users having tha same college as current users...

Following is the model codes :

    class User < ActiveRecord::Base
        has_many :welcomes
        has_many :educations
        has_many :colleges, :through => :educations   
    end

    class College < ActiveRecord::Base
        has_many :educations
        has_many :users, :through => :educations
    end

    class Welcome < ActiveRecord::Base
        belongs_to :user
    end
 
    class Education < ActiveRecord::Base
    belongs_to :user
    belongs_to :college
    end

Following is my schema code for these models :

    create_table "welcomes", :force => true do |t|
        t.text     "message"
        t.integer  "user_id"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

    create_table "users", :force => true do |t|
        t.string   "email",                                 :default => "", :null => false
        t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",                         :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.string   "first_name"
        t.string   "last_name"
        t.integer  "role_id"
        t.string   "ancestry"
      end
    create_table "educations", :force => true do |t|
        t.integer  "college_id"
        t.integer  "user_id"
        t.integer  "year"
        t.text     "concentration"
        t.string   "attended_for"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.string   "month"
        t.boolean  "private",         :default => false
        t.boolean  "approved",        :default => false
        t.integer  "approved_by"
        t.boolean  "alumni",          :default => false
        t.boolean  "is_mentor",       :default => false
        t.boolean  "approved_mentor", :default => false
      end
   
    create_table "colleges", :force => true do |t|
        t.string   "name"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.boolean  "approved",   :default => false
      end

Re: how to find records from another table in rails?

What type of a results does this line get you?

@college = User.find(@current_user.id).colleges

Re: how to find records from another table in rails?

Iscott3.. that line returns me college data of current user....

Last edited by nikita (2012-04-05 00:39:30)