views:

38

answers:

3

Hello, How can I define a relationship between two controllers. I have one controller called rides and another called registrant. Is there anyway I can access the registrant database from within the rides controller? I was thinking

@registrant = Registrant.find(:first)

from within rides, but that didn't work. Any suggestions?

Thanks

+2  A: 

You can access your registrant model from your rides controller just like accessing it from any other controller. What do you mean by Registrant.find(:first) not working?

Now, if there's a relationship (or association as it's normally called) between your rides model and registrant model (like a has_many association), you can use nested resources to nest one controller in another.

Check out the Action Controller Overview and Rails Routing from the Outside In guides and think about picking up a good book on Rails like Agile Web Development with Rails.

Mark A. Nicolosi
Well it turns out I just had a capitalization error and it was getting to the Registrant model fine! Thanks for the help though!
Pat R
A: 

If you have defined models: ride and registrant (or more general user) then you can setup a before_filter on the rides controller:

before_filter :get_user

def get_user
    @user = User.find(:first, :conditions => %Q(userid = "#{params[:user_id]}"))
end

This would fetch the the user with user_id passed in as a parameter before the controller generates the view.

ennuikiller
That doesn't seem to answer his question and more simply you could do `User.find(params[:user_id]` or am I missing something?
Mark A. Nicolosi
Oops, missed a right paren in there.
Mark A. Nicolosi
A: 

Yes, that should work. To get the terminology right, you are accessing the Registrant model from the RidesController. They should both be in the same database, but in separate tables.

Please post the error message you are getting.

MattMcKnight