views:

40

answers:

2

I have an application with three Models (Profile -> SubModel -> SubSubModel) chained together with has many relationships. I am trying to limit a user, after logging in, to only retrieving records that are associated with their Profile. I am very new to rails and this is what I had been trying in the Profile model

has_many :submodels, :conditions => {:profile_id => self.id}

but this is returning an empty data set when calling with Profile.find_by_id(1).submodels, how else could I achieve what I am trying to do. Or should I handle this in the controller or view instead, I thought it sounded well suited for the model to handle this.

+1  A: 

you don't need any conditions on the has_many call - by default it will only return the SubModels associated with the Profile.

emh
Well thats what I get for not thinking...
trobrock
A: 

If you've named your classes and foreign/primary keys to Rails conventions, just use

class Profile
  has_many :sub_models
end

and let Rails figure it out.

This assumes the following:

  • Profile wraps a table named profiles, which has a numeric primary key named id
  • SubModel wraps a table named sub_models, which has a numeric foreign key named profile_id
meagar