Hi everyone, I have users. Users have many affiliations and one primary affiliation. The affiliations and primary affiliations come from the same pool of data, so I just want to point primary affiliations to the affiliations model. Here''s what I have (irrelevant pieces snipped): class User < ActiveRecord::Base has_many :affiliations has_one :primary_affiliation, :class_name => "Affiliation", :foreign_key => "id" end class Affiliation < ActiveRecord::Base has_and_belongs_to_many :users end The users table has a primary_affiliation column. The affiliations table has an id, a code, and a description. When I have a user object, I''d like to be able to get: user.primary_affiliation.code But I end up getting a NilClass error: undefined method `code'' for nil:NilClass What''s the teeny-tiny thing I''m missing? Thanks! Sean
I think you want class User < ActiveRecord::Base has_many :affiliations has_one :primary_affiliation, :class_name => "Affiliation", :foreign_key => "primary_affiliation_id" end And to change the `users.primary_affiliation` column to be `users.primary_affiliation_id`. I think that should clear up your WhinyNil error. -jon On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi everyone, > > I have users. Users have many affiliations and one primary > affiliation. The affiliations and primary affiliations come from the > same pool of data, so I just want to point primary affiliations to the > affiliations model. Here''s what I have (irrelevant pieces snipped): > > class User < ActiveRecord::Base > has_many :affiliations > has_one :primary_affiliation, :class_name => "Affiliation", > :foreign_key => "id" > end > > class Affiliation < ActiveRecord::Base > has_and_belongs_to_many :users > end > > The users table has a primary_affiliation column. The affiliations > table has an id, a code, and a description. > > When I have a user object, I''d like to be able to get: > > user.primary_affiliation.code > > But I end up getting a NilClass error: > > undefined method `code'' for nil:NilClass > > What''s the teeny-tiny thing I''m missing? > > Thanks! > > Sean > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ok, I set the model like that and changed the column in the database to be primary_affiliation_id. If I try to call user.primary_affiliation_id, I get the id number. If I try to get the code that the id number represents (user.primary_affiliation_id.code), I get: undefined method `code'' for 1:Fixnum I think I still have the relationship mixed up somehow. Sean On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote:> I think you want > > class User < ActiveRecord::Base > has_many :affiliations > has_one :primary_affiliation, :class_name => "Affiliation", > :foreign_key => "primary_affiliation_id" > end > > And to change the `users.primary_affiliation` column to be > `users.primary_affiliation_id`. > > I think that should clear up your WhinyNil error. > > -jon > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi everyone, > > > > I have users. Users have many affiliations and one primary > > affiliation. The affiliations and primary affiliations come from the > > same pool of data, so I just want to point primary affiliations to the > > affiliations model. Here''s what I have (irrelevant pieces snipped): > > > > class User < ActiveRecord::Base > > has_many :affiliations > > has_one :primary_affiliation, :class_name => "Affiliation", > > :foreign_key => "id" > > end > > > > class Affiliation < ActiveRecord::Base > > has_and_belongs_to_many :users > > end > > > > The users table has a primary_affiliation column. The affiliations > > table has an id, a code, and a description. > > > > When I have a user object, I''d like to be able to get: > > > > user.primary_affiliation.code > > > > But I end up getting a NilClass error: > > > > undefined method `code'' for nil:NilClass > > > > What''s the teeny-tiny thing I''m missing? > > > > Thanks! > > > > Sean > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Right, the method name that the `has_one` call generates is the first parameter, the :primary_affiliation. So the method you''d call to get back the primary affiliation object is user.primary_affilitation That''ll return the object, or nil if there''s no association. On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok, I set the model like that and changed the column in the database > to be primary_affiliation_id. If I try to call > user.primary_affiliation_id, I get the id number. If I try to get the > code that the id number represents (user.primary_affiliation_id.code), > I get: > > undefined method `code'' for 1:Fixnum > > I think I still have the relationship mixed up somehow. > > Sean > > On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote: > > I think you want > > > > class User < ActiveRecord::Base > > has_many :affiliations > > has_one :primary_affiliation, :class_name => "Affiliation", > > :foreign_key => "primary_affiliation_id" > > end > > > > And to change the `users.primary_affiliation` column to be > > `users.primary_affiliation_id`. > > > > I think that should clear up your WhinyNil error. > > > > -jon > > > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi everyone, > > > > > > I have users. Users have many affiliations and one primary > > > affiliation. The affiliations and primary affiliations come from the > > > same pool of data, so I just want to point primary affiliations to the > > > affiliations model. Here''s what I have (irrelevant pieces snipped): > > > > > > class User < ActiveRecord::Base > > > has_many :affiliations > > > has_one :primary_affiliation, :class_name => "Affiliation", > > > :foreign_key => "id" > > > end > > > > > > class Affiliation < ActiveRecord::Base > > > has_and_belongs_to_many :users > > > end > > > > > > The users table has a primary_affiliation column. The affiliations > > > table has an id, a code, and a description. > > > > > > When I have a user object, I''d like to be able to get: > > > > > > user.primary_affiliation.code > > > > > > But I end up getting a NilClass error: > > > > > > undefined method `code'' for nil:NilClass > > > > > > What''s the teeny-tiny thing I''m missing? > > > > > > Thanks! > > > > > > Sean > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Then I get this: Unknown column ''affiliations.primary_affiliation_id'' in ''where clause'': SELECT * FROM affiliations WHERE affiliations.primary_affiliation_id = 372 LIMIT 1 On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote:> Right, the method name that the `has_one` call generates is the first > parameter, the :primary_affiliation. > > So the method you''d call to get back the primary affiliation object is > > user.primary_affilitation > > That''ll return the object, or nil if there''s no association. > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Ok, I set the model like that and changed the column in the database > > to be primary_affiliation_id. If I try to call > > user.primary_affiliation_id, I get the id number. If I try to get the > > code that the id number represents (user.primary_affiliation_id.code), > > I get: > > > > undefined method `code'' for 1:Fixnum > > > > I think I still have the relationship mixed up somehow. > > > > Sean > > > > On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote: > > > I think you want > > > > > > class User < ActiveRecord::Base > > > has_many :affiliations > > > has_one :primary_affiliation, :class_name => "Affiliation", > > > :foreign_key => "primary_affiliation_id" > > > end > > > > > > And to change the `users.primary_affiliation` column to be > > > `users.primary_affiliation_id`. > > > > > > I think that should clear up your WhinyNil error. > > > > > > -jon > > > > > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi everyone, > > > > > > > > I have users. Users have many affiliations and one primary > > > > affiliation. The affiliations and primary affiliations come from the > > > > same pool of data, so I just want to point primary affiliations to the > > > > affiliations model. Here''s what I have (irrelevant pieces snipped): > > > > > > > > class User < ActiveRecord::Base > > > > has_many :affiliations > > > > has_one :primary_affiliation, :class_name => "Affiliation", > > > > :foreign_key => "id" > > > > end > > > > > > > > class Affiliation < ActiveRecord::Base > > > > has_and_belongs_to_many :users > > > > end > > > > > > > > The users table has a primary_affiliation column. The affiliations > > > > table has an id, a code, and a description. > > > > > > > > When I have a user object, I''d like to be able to get: > > > > > > > > user.primary_affiliation.code > > > > > > > > But I end up getting a NilClass error: > > > > > > > > undefined method `code'' for nil:NilClass > > > > > > > > What''s the teeny-tiny thing I''m missing? > > > > > > > > Thanks! > > > > > > > > Sean > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Eureka! For completness in the archive, this is how it was fixed. Change "has_one" to "belongs_to": belongs_to :primary_affiliation, :class_name => "Affiliation", :foreign_key => "primary_affiliation_id" Access like so: user.primary_affiliation.code Thanks, Jon! Sean On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote:> RIght. Teach me to not read your code with my whole brain. You need > `belongs_to`, rather than `has_one` if the key is going to live in the > `users` table. > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Then I get this: > > > > Unknown column ''affiliations.primary_affiliation_id'' in ''where > > clause'': SELECT * FROM affiliations WHERE > > affiliations.primary_affiliation_id = 372 LIMIT 1 > > > > On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote: > > > Right, the method name that the `has_one` call generates is the first > > > parameter, the :primary_affiliation. > > > > > > So the method you''d call to get back the primary affiliation object is > > > > > > user.primary_affilitation > > > > > > That''ll return the object, or nil if there''s no association. > > > > > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Ok, I set the model like that and changed the column in the database > > > > to be primary_affiliation_id. If I try to call > > > > user.primary_affiliation_id, I get the id number. If I try to get the > > > > code that the id number represents (user.primary_affiliation_id.code), > > > > I get: > > > > > > > > undefined method `code'' for 1:Fixnum > > > > > > > > I think I still have the relationship mixed up somehow. > > > > > > > > Sean > > > > > > > > On 10/6/05, Jon <jon-FY6cS9nie8kfmlEeYq7lZQ@public.gmane.org> wrote: > > > > > I think you want > > > > > > > > > > class User < ActiveRecord::Base > > > > > has_many :affiliations > > > > > has_one :primary_affiliation, :class_name => "Affiliation", > > > > > :foreign_key => "primary_affiliation_id" > > > > > end > > > > > > > > > > And to change the `users.primary_affiliation` column to be > > > > > `users.primary_affiliation_id`. > > > > > > > > > > I think that should clear up your WhinyNil error. > > > > > > > > > > -jon > > > > > > > > > > On 10/6/05, Sean Hussey <seanhussey-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hi everyone, > > > > > > > > > > > > I have users. Users have many affiliations and one primary > > > > > > affiliation. The affiliations and primary affiliations come from the > > > > > > same pool of data, so I just want to point primary affiliations to the > > > > > > affiliations model. Here''s what I have (irrelevant pieces snipped): > > > > > > > > > > > > class User < ActiveRecord::Base > > > > > > has_many :affiliations > > > > > > has_one :primary_affiliation, :class_name => "Affiliation", > > > > > > :foreign_key => "id" > > > > > > end > > > > > > > > > > > > class Affiliation < ActiveRecord::Base > > > > > > has_and_belongs_to_many :users > > > > > > end > > > > > > > > > > > > The users table has a primary_affiliation column. The affiliations > > > > > > table has an id, a code, and a description. > > > > > > > > > > > > When I have a user object, I''d like to be able to get: > > > > > > > > > > > > user.primary_affiliation.code > > > > > > > > > > > > But I end up getting a NilClass error: > > > > > > > > > > > > undefined method `code'' for nil:NilClass > > > > > > > > > > > > What''s the teeny-tiny thing I''m missing? > > > > > > > > > > > > Thanks! > > > > > > > > > > > > Sean > > > > > > _______________________________________________ > > > > > > Rails mailing list > > > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > >