• 0

RoR Database Retrieval


Question

Hi guys, I am running into some problems when I retrieve my users in a Ruby on Rails application. Everything is working great, but when I try to do a before_filter :get_users the app is crashing due to an error of mine in the logic. I want to retrieve all users that belong to a certain institution, using this models:


class User < ActiveRecord::Base
belongs_to :institution
end

class Institution < ActiveRecord::Base
has_many :users
end

class HomeController < ApplicationController
before_filter :get_users

def get_users
@users = User.where("institution" => current_user.institution)
end
end
[/CODE]

Can please someone help me out I've tried everything without any luck. Thank you in advance.

Link to comment
https://www.neowin.net/forum/topic/1078353-ror-database-retrieval/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Note: The above probably won't be doing what you think it's doing. Unless current_user.institution is equal to the string "institution" (in your case it never is) it will evaluate to:

@users = User.joins(:institution).where(false)[/CODE]

which returns [b]all[/b] the users (with an institute). Instead you probably want to do:

[CODE]User.where(:institution_id => current_user.institution)[/CODE]

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.