I''m writing my first simple app in Rails. I''ve got a list page working and a new_entry page working. My main table (cyclog--"bicycle log") holds references to keys from two other tables that are just lists with "id" and "description". When I display my entrieds, I want to show the value of "description" rather than "id". I''ve tried using has_one and composed_of, but to no avail. I''m wondering what I might be doing wrong. In cyclog model I have: class Cyclog < ActiveRecord::Base has_one :mood has_one :energy_level #composed_of :mood, :mapping => %w(mood_description description) end When I reference mood.description in my rhtml, I get this error: Showing /cyclog/list.rhtml where line #19 raised undefined method `description'' for nil:NilClass and here''s the rhtml code: 19: <td><%= @item.mood.description %></td> if I change up to use composed_of and @item.mood_description, I get an error as well. Thanks, Jamie
asking for help again... ----- Original Message ----- From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Sunday, January 09, 2005 10:39 PM Subject: [Rails] newbie: has_one? composed_of?> I''m writing my first simple app in Rails. I''ve got a list page working > and a new_entry page working. My main table (cyclog--"bicycle log") > holds references to keys from two other tables that are just lists with > "id" and "description". When I display my entrieds, I want to show the > value of "description" rather than "id". I''ve tried using has_one and > composed_of, but to no avail. I''m wondering what I might be doing > wrong. > > In cyclog model I have: > > class Cyclog < ActiveRecord::Base > has_one :mood > has_one :energy_level > #composed_of :mood, :mapping => %w(mood_description description) > end > > When I reference mood.description in my rhtml, I get this error: > > Showing /cyclog/list.rhtml where line #19 raised undefined method > `description'' for nil:NilClass > > and here''s the rhtml code: > > 19: <td><%= @item.mood.description %></td> > > if I change up to use composed_of and @item.mood_description, I get an > error as well. > > Thanks, > > Jamie > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Have you read ActiveRecord documentation on : Is it belongs_to or has_one? Both express a 1-1 relationship, the difference is mostly where to place the foreign key, which goes on the table for the class saying belongs_to. Example: class Post < ActiveRecord::Base has_one :author end class Author < ActiveRecord::Base belongs_to :post end The tables for these classes could look something like: CREATE TABLE posts ( id int(11) NOT NULL auto_increment, title varchar default NULL, PRIMARY KEY (id) ) CREATE TABLE authors ( id int(11) NOT NULL auto_increment, post_id int(11) default NULL, name varchar default NULL, PRIMARY KEY (id) ) Hope it help you!!! Colomain. On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays <jamie-fswG1Ka7Iew@public.gmane.org> wrote:> asking for help again... > > > ----- Original Message ----- > From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Sent: Sunday, January 09, 2005 10:39 PM > Subject: [Rails] newbie: has_one? composed_of? > > > I''m writing my first simple app in Rails. I''ve got a list page working > > and a new_entry page working. My main table (cyclog--"bicycle log") > > holds references to keys from two other tables that are just lists with > > "id" and "description". When I display my entrieds, I want to show the > > value of "description" rather than "id". I''ve tried using has_one and > > composed_of, but to no avail. I''m wondering what I might be doing > > wrong. > > > > In cyclog model I have: > > > > class Cyclog < ActiveRecord::Base > > has_one :mood > > has_one :energy_level > > #composed_of :mood, :mapping => %w(mood_description description) > > end > > > > When I reference mood.description in my rhtml, I get this error: > > > > Showing /cyclog/list.rhtml where line #19 raised undefined method > > `description'' for nil:NilClass > > > > and here''s the rhtml code: > > > > 19: <td><%= @item.mood.description %></td> > > > > if I change up to use composed_of and @item.mood_description, I get an > > error as well. > > > > Thanks, > > > > Jamie > > > > _______________________________________________ > > 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 >
So in order to describe this kind of relationship, one class needs has_one and the other needs belongs_to ? Or is it an either/or situation? Bill On Mon, 10 Jan 2005 17:19:10 +0100, Colomain <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Have you read ActiveRecord documentation on : > > Is it belongs_to or has_one? > > Both express a 1-1 relationship, the difference is mostly where to > place the foreign key, which goes on the table for the class saying > belongs_to. Example: > > class Post < ActiveRecord::Base > has_one :author > end > > class Author < ActiveRecord::Base > belongs_to :post > end > > The tables for these classes could look something like: > > CREATE TABLE posts ( > id int(11) NOT NULL auto_increment, > title varchar default NULL, > PRIMARY KEY (id) > ) > > CREATE TABLE authors ( > id int(11) NOT NULL auto_increment, > post_id int(11) default NULL, > name varchar default NULL, > PRIMARY KEY (id) > ) > > Hope it help you!!! > Colomain. > > > On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays <jamie-fswG1Ka7Iew@public.gmane.org> wrote: > > asking for help again... > > > > > > ----- Original Message ----- > > From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> > > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > > Sent: Sunday, January 09, 2005 10:39 PM > > Subject: [Rails] newbie: has_one? composed_of? > > > > > I''m writing my first simple app in Rails. I''ve got a list page working > > > and a new_entry page working. My main table (cyclog--"bicycle log") > > > holds references to keys from two other tables that are just lists with > > > "id" and "description". When I display my entrieds, I want to show the > > > value of "description" rather than "id". I''ve tried using has_one and > > > composed_of, but to no avail. I''m wondering what I might be doing > > > wrong. > > > > > > In cyclog model I have: > > > > > > class Cyclog < ActiveRecord::Base > > > has_one :mood > > > has_one :energy_level > > > #composed_of :mood, :mapping => %w(mood_description description) > > > end > > > > > > When I reference mood.description in my rhtml, I get this error: > > > > > > Showing /cyclog/list.rhtml where line #19 raised undefined method > > > `description'' for nil:NilClass > > > > > > and here''s the rhtml code: > > > > > > 19: <td><%= @item.mood.description %></td> > > > > > > if I change up to use composed_of and @item.mood_description, I get an > > > error as well. > > > > > > Thanks, > > > > > > Jamie > > > > > > _______________________________________________ > > > 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 > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- $stdout.sync = true "Just another Ruby hacker.".each_byte do |b| (''a''..''z'').step do|c|print c+"\b";sleep 0.007 end;print b.chr end; print "\n"
Hi Colomain,> Have you read ActiveRecord documentation on : > > Is it belongs_to or has_one? > > Both express a 1-1 relationship, the difference is mostly where to > place the foreign key, which goes on the table for the class saying > belongs_to. Example: > > class Post < ActiveRecord::Base > has_one :author > end > > class Author < ActiveRecord::Base > belongs_to :post > endThis is something I see difficulty with, my lack of knowledge about DB design, in my mind it makes more sense to say that an Author has_many posts and a Post belongs_to Author. An author will write posts but a post cannot exist without an author which means that the post should link to the author_id. No? This is a problem I having reading some of the examples, the relationships don''t "click" for me; like here. Rob
Yup. I followed the examples in the documentation precisely. That''s why I''m at a loss. Thanks! Jamie ----- Original Message ----- From: "Colomain" <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Monday, January 10, 2005 11:19 AM Subject: Re: [Rails] newbie: has_one? composed_of?> Have you read ActiveRecord documentation on : > > Is it belongs_to or has_one? > > Both express a 1-1 relationship, the difference is mostly where to > place the foreign key, which goes on the table for the class saying > belongs_to. Example: > > class Post < ActiveRecord::Base > has_one :author > end > > class Author < ActiveRecord::Base > belongs_to :post > end > > The tables for these classes could look something like: > > CREATE TABLE posts ( > id int(11) NOT NULL auto_increment, > title varchar default NULL, > PRIMARY KEY (id) > ) > > CREATE TABLE authors ( > id int(11) NOT NULL auto_increment, > post_id int(11) default NULL, > name varchar default NULL, > PRIMARY KEY (id) > ) > > > Hope it help you!!! > Colomain. > > > On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays <jamie-fswG1Ka7Iew@public.gmane.org> > wrote: >> asking for help again... >> >> >> ----- Original Message ----- >> From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> >> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >> Sent: Sunday, January 09, 2005 10:39 PM >> Subject: [Rails] newbie: has_one? composed_of? >> >> > I''m writing my first simple app in Rails. I''ve got a list page working >> > and a new_entry page working. My main table (cyclog--"bicycle log") >> > holds references to keys from two other tables that are just lists with >> > "id" and "description". When I display my entrieds, I want to show the >> > value of "description" rather than "id". I''ve tried using has_one and >> > composed_of, but to no avail. I''m wondering what I might be doing >> > wrong. >> > >> > In cyclog model I have: >> > >> > class Cyclog < ActiveRecord::Base >> > has_one :mood >> > has_one :energy_level >> > #composed_of :mood, :mapping => %w(mood_description description) >> > end >> > >> > When I reference mood.description in my rhtml, I get this error: >> > >> > Showing /cyclog/list.rhtml where line #19 raised undefined method >> > `description'' for nil:NilClass >> > >> > and here''s the rhtml code: >> > >> > 19: <td><%= @item.mood.description %></td> >> > >> > if I change up to use composed_of and @item.mood_description, I get an >> > error as well. >> > >> > Thanks, >> > >> > Jamie >> > >> > _______________________________________________ >> > 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 >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
I know it rather obvious, but do you have valid records in your database. Specifically is there a row in your ''mood'' table with an ''id'' that matches the ''mood_id'' column in your ''cyclelog'' table. On Jan 10, 2005, at 8:59 AM, Jamie Orchard-Hays wrote:> Yup. I followed the examples in the documentation precisely. That''s > why I''m at a loss. > > Thanks! > > Jamie > > > ----- Original Message ----- From: "Colomain" <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Sent: Monday, January 10, 2005 11:19 AM > Subject: Re: [Rails] newbie: has_one? composed_of? > > >> Have you read ActiveRecord documentation on : >> >> Is it belongs_to or has_one? >> >> Both express a 1-1 relationship, the difference is mostly where to >> place the foreign key, which goes on the table for the class saying >> belongs_to. Example: >> >> class Post < ActiveRecord::Base >> has_one :author >> end >> >> class Author < ActiveRecord::Base >> belongs_to :post >> end >> >> The tables for these classes could look something like: >> >> CREATE TABLE posts ( >> id int(11) NOT NULL auto_increment, >> title varchar default NULL, >> PRIMARY KEY (id) >> ) >> >> CREATE TABLE authors ( >> id int(11) NOT NULL auto_increment, >> post_id int(11) default NULL, >> name varchar default NULL, >> PRIMARY KEY (id) >> ) >> >> >> Hope it help you!!! >> Colomain. >> >> >> On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays >> <jamie-fswG1Ka7Iew@public.gmane.org> wrote: >>> asking for help again... >>> >>> >>> ----- Original Message ----- >>> From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> >>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Sent: Sunday, January 09, 2005 10:39 PM >>> Subject: [Rails] newbie: has_one? composed_of? >>> >>> > I''m writing my first simple app in Rails. I''ve got a list page >>> working >>> > and a new_entry page working. My main table (cyclog--"bicycle log") >>> > holds references to keys from two other tables that are just lists >>> with >>> > "id" and "description". When I display my entrieds, I want to show >>> the >>> > value of "description" rather than "id". I''ve tried using has_one >>> and >>> > composed_of, but to no avail. I''m wondering what I might be doing >>> > wrong. >>> > >>> > In cyclog model I have: >>> > >>> > class Cyclog < ActiveRecord::Base >>> > has_one :mood >>> > has_one :energy_level >>> > #composed_of :mood, :mapping => %w(mood_description >>> description) >>> > end >>> > >>> > When I reference mood.description in my rhtml, I get this error: >>> > >>> > Showing /cyclog/list.rhtml where line #19 raised undefined method >>> > `description'' for nil:NilClass >>> > >>> > and here''s the rhtml code: >>> > >>> > 19: <td><%= @item.mood.description %></td> >>> > >>> > if I change up to use composed_of and @item.mood_description, I >>> get an >>> > error as well. >>> > >>> > Thanks, >>> > >>> > Jamie >>> > >>> > _______________________________________________ >>> > 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 >>> >> _______________________________________________ >> 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 >
There sure is! moods has id cyclog has mood_id energy_levels has id cyclog has energy_level_id Thanks, Jamie ----- Original Message ----- From: "Steve Zich" <szich-Cs8BDP4EB0lWk0Htik3J/w@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Monday, January 10, 2005 12:04 PM Subject: Re: [Rails] newbie: has_one? composed_of?>I know it rather obvious, but do you have valid records in your > database. Specifically is there a row in your ''mood'' table with an > ''id'' that matches the ''mood_id'' column in your ''cyclelog'' table. > > On Jan 10, 2005, at 8:59 AM, Jamie Orchard-Hays wrote: > >> Yup. I followed the examples in the documentation precisely. That''s >> why I''m at a loss. >> >> Thanks! >> >> Jamie >> >> >> ----- Original Message ----- From: "Colomain" <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >> Sent: Monday, January 10, 2005 11:19 AM >> Subject: Re: [Rails] newbie: has_one? composed_of? >> >> >>> Have you read ActiveRecord documentation on : >>> >>> Is it belongs_to or has_one? >>> >>> Both express a 1-1 relationship, the difference is mostly where to >>> place the foreign key, which goes on the table for the class saying >>> belongs_to. Example: >>> >>> class Post < ActiveRecord::Base >>> has_one :author >>> end >>> >>> class Author < ActiveRecord::Base >>> belongs_to :post >>> end >>> >>> The tables for these classes could look something like: >>> >>> CREATE TABLE posts ( >>> id int(11) NOT NULL auto_increment, >>> title varchar default NULL, >>> PRIMARY KEY (id) >>> ) >>> >>> CREATE TABLE authors ( >>> id int(11) NOT NULL auto_increment, >>> post_id int(11) default NULL, >>> name varchar default NULL, >>> PRIMARY KEY (id) >>> ) >>> >>> >>> Hope it help you!!! >>> Colomain. >>> >>> >>> On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays >>> <jamie-fswG1Ka7Iew@public.gmane.org> wrote: >>>> asking for help again... >>>> >>>> >>>> ----- Original Message ----- >>>> From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> >>>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>>> Sent: Sunday, January 09, 2005 10:39 PM >>>> Subject: [Rails] newbie: has_one? composed_of? >>>> >>>> > I''m writing my first simple app in Rails. I''ve got a list page >>>> working >>>> > and a new_entry page working. My main table (cyclog--"bicycle log") >>>> > holds references to keys from two other tables that are just lists >>>> with >>>> > "id" and "description". When I display my entrieds, I want to show >>>> the >>>> > value of "description" rather than "id". I''ve tried using has_one >>>> and >>>> > composed_of, but to no avail. I''m wondering what I might be doing >>>> > wrong. >>>> > >>>> > In cyclog model I have: >>>> > >>>> > class Cyclog < ActiveRecord::Base >>>> > has_one :mood >>>> > has_one :energy_level >>>> > #composed_of :mood, :mapping => %w(mood_description >>>> description) >>>> > end >>>> > >>>> > When I reference mood.description in my rhtml, I get this error: >>>> > >>>> > Showing /cyclog/list.rhtml where line #19 raised undefined method >>>> > `description'' for nil:NilClass >>>> > >>>> > and here''s the rhtml code: >>>> > >>>> > 19: <td><%= @item.mood.description %></td> >>>> > >>>> > if I change up to use composed_of and @item.mood_description, I >>>> get an >>>> > error as well. >>>> > >>>> > Thanks, >>>> > >>>> > Jamie >>>> > >>>> > _______________________________________________ >>>> > 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 >>>> >>> _______________________________________________ >>> 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 >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> > class Post < ActiveRecord::Base > > has_one :author > > end > > > > class Author < ActiveRecord::Base > > belongs_to :post > > end > > This is something I see difficulty with, my lack of knowledge about DB > design, in my mind it makes more sense to say that an Author has_many > posts and a Post belongs_to Author. > > An author will write posts but a post cannot exist without an author > which means that the post should link to the author_id. No? > > This is a problem I having reading some of the examples, the > relationships don''t "click" for me; like here.Yeah, some of them are not quite right, Perhaps you could file a ticket listing all the ''inconsistent'' examples and we can fix them before 1.0.> Rob > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On Mon, 10 Jan 2005 11:32:45 -0500, Bill Atkins <batkins57-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So in order to describe this kind of relationship, one class needs > has_one and the other needs belongs_to ? Or is it an either/or > situation?With your classes, an association has both cardinaltiy (has_one or has_many) and directionality (which direction can I navigated this association). But with your tables, there is no directionality, and cardinality is only there in some cases. Say you have a 1-1 association between users and addresses, you may want to say: class User < ActiveRecord::Base has_one :address end class Address < ActiveRecord::Base belongs_to :user end This means if you have a user you can find its address, and if you have an address you can find its user. But if you never need to navigate from address to user (i.e you never do an address search or something) you could leave off that end of the association and just say class User < ActiveRecord::Base has_one :address end class Address < ActiveRecord::Base end So in answer to your question, it''s not either or, nor is it both, its "What your domain model requires"> Bill > > > On Mon, 10 Jan 2005 17:19:10 +0100, Colomain <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Have you read ActiveRecord documentation on : > > > > Is it belongs_to or has_one? > > > > Both express a 1-1 relationship, the difference is mostly where to > > place the foreign key, which goes on the table for the class saying > > belongs_to. Example: > > > > class Post < ActiveRecord::Base > > has_one :author > > end > > > > class Author < ActiveRecord::Base > > belongs_to :post > > end > > > > The tables for these classes could look something like: > > > > CREATE TABLE posts ( > > id int(11) NOT NULL auto_increment, > > title varchar default NULL, > > PRIMARY KEY (id) > > ) > > > > CREATE TABLE authors ( > > id int(11) NOT NULL auto_increment, > > post_id int(11) default NULL, > > name varchar default NULL, > > PRIMARY KEY (id) > > ) > > > > Hope it help you!!! > > Colomain. > > > > > > On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays <jamie-fswG1Ka7Iew@public.gmane.org> wrote: > > > asking for help again... > > > > > > > > > ----- Original Message ----- > > > From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> > > > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > > > Sent: Sunday, January 09, 2005 10:39 PM > > > Subject: [Rails] newbie: has_one? composed_of? > > > > > > > I''m writing my first simple app in Rails. I''ve got a list page working > > > > and a new_entry page working. My main table (cyclog--"bicycle log") > > > > holds references to keys from two other tables that are just lists with > > > > "id" and "description". When I display my entrieds, I want to show the > > > > value of "description" rather than "id". I''ve tried using has_one and > > > > composed_of, but to no avail. I''m wondering what I might be doing > > > > wrong. > > > > > > > > In cyclog model I have: > > > > > > > > class Cyclog < ActiveRecord::Base > > > > has_one :mood > > > > has_one :energy_level > > > > #composed_of :mood, :mapping => %w(mood_description description) > > > > end > > > > > > > > When I reference mood.description in my rhtml, I get this error: > > > > > > > > Showing /cyclog/list.rhtml where line #19 raised undefined method > > > > `description'' for nil:NilClass > > > > > > > > and here''s the rhtml code: > > > > > > > > 19: <td><%= @item.mood.description %></td> > > > > > > > > if I change up to use composed_of and @item.mood_description, I get an > > > > error as well. > > > > > > > > Thanks, > > > > > > > > Jamie > > > > > > > > _______________________________________________ > > > > 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 > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > $stdout.sync = true > "Just another Ruby hacker.".each_byte do |b| > (''a''..''z'').step do|c|print c+"\b";sleep 0.007 end;print b.chr > end; print "\n" > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
I wonder if this is a typical newbie error: I had reversed the concept of "has_one" and "belongs_to". I finally figured it out after playing with the models and looking at the log file. Thanks, Jamie On Jan 10, 2005, at 1:08 PM, Michael Koziarski wrote:> On Mon, 10 Jan 2005 11:32:45 -0500, Bill Atkins <batkins57-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> So in order to describe this kind of relationship, one class needs >> has_one and the other needs belongs_to ? Or is it an either/or >> situation? > > With your classes, an association has both cardinaltiy (has_one or > has_many) and directionality (which direction can I navigated this > association). But with your tables, there is no directionality, and > cardinality is only there in some cases. > > Say you have a 1-1 association between users and addresses, you may > want to say: > > class User < ActiveRecord::Base > has_one :address > end > class Address < ActiveRecord::Base > belongs_to :user > end > > This means if you have a user you can find its address, and if you > have an address you can find its user. But if you never need to > navigate from address to user (i.e you never do an address search or > something) you could leave off that end of the association and just > say > > class User < ActiveRecord::Base > has_one :address > end > class Address < ActiveRecord::Base > end > > So in answer to your question, it''s not either or, nor is it both, > its "What your domain model requires" > >> Bill >> >> >> On Mon, 10 Jan 2005 17:19:10 +0100, Colomain <colomain-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >>> Have you read ActiveRecord documentation on : >>> >>> Is it belongs_to or has_one? >>> >>> Both express a 1-1 relationship, the difference is mostly where to >>> place the foreign key, which goes on the table for the class saying >>> belongs_to. Example: >>> >>> class Post < ActiveRecord::Base >>> has_one :author >>> end >>> >>> class Author < ActiveRecord::Base >>> belongs_to :post >>> end >>> >>> The tables for these classes could look something like: >>> >>> CREATE TABLE posts ( >>> id int(11) NOT NULL auto_increment, >>> title varchar default NULL, >>> PRIMARY KEY (id) >>> ) >>> >>> CREATE TABLE authors ( >>> id int(11) NOT NULL auto_increment, >>> post_id int(11) default NULL, >>> name varchar default NULL, >>> PRIMARY KEY (id) >>> ) >>> >>> Hope it help you!!! >>> Colomain. >>> >>> >>> On Mon, 10 Jan 2005 11:01:04 -0500, Jamie Orchard-Hays >>> <jamie-fswG1Ka7Iew@public.gmane.org> wrote: >>>> asking for help again... >>>> >>>> >>>> ----- Original Message ----- >>>> From: "Jamie Orchard-Hays" <jamie-fswG1Ka7Iew@public.gmane.org> >>>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>>> Sent: Sunday, January 09, 2005 10:39 PM >>>> Subject: [Rails] newbie: has_one? composed_of? >>>> >>>>> I''m writing my first simple app in Rails. I''ve got a list page >>>>> working >>>>> and a new_entry page working. My main table (cyclog--"bicycle log") >>>>> holds references to keys from two other tables that are just lists >>>>> with >>>>> "id" and "description". When I display my entrieds, I want to show >>>>> the >>>>> value of "description" rather than "id". I''ve tried using has_one >>>>> and >>>>> composed_of, but to no avail. I''m wondering what I might be doing >>>>> wrong. >>>>> >>>>> In cyclog model I have: >>>>> >>>>> class Cyclog < ActiveRecord::Base >>>>> has_one :mood >>>>> has_one :energy_level >>>>> #composed_of :mood, :mapping => %w(mood_description >>>>> description) >>>>> end >>>>> >>>>> When I reference mood.description in my rhtml, I get this error: >>>>> >>>>> Showing /cyclog/list.rhtml where line #19 raised undefined method >>>>> `description'' for nil:NilClass >>>>> >>>>> and here''s the rhtml code: >>>>> >>>>> 19: <td><%= @item.mood.description %></td> >>>>> >>>>> if I change up to use composed_of and @item.mood_description, I >>>>> get an >>>>> error as well. >>>>> >>>>> Thanks, >>>>> >>>>> Jamie >>>>> >>>>> _______________________________________________ >>>>> 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 >>>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> >> -- >> $stdout.sync = true >> "Just another Ruby hacker.".each_byte do |b| >> (''a''..''z'').step do|c|print c+"\b";sleep 0.007 end;print b.chr >> end; print "\n" >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Cheers > > Koz > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jamie Orchard-Hays wrote: | I wonder if this is a typical newbie error: I had reversed the concept | of "has_one" and "belongs_to". I finally figured it out after playing | with the models and looking at the log file. | | Thanks, | | Jamie I drew up a little diagram when I first started doing Rails, to keep the relationships straight in my head. If you haven''t come across it yet, you can see it at http://curry.elitists.net/~scott/ARAssociations.png Enjoy! - -Scott -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFB41NAvqYRQQGcpIQRAvUMAJ9drcvp24XhxaujHSvp4nM8UJ4i4wCfZA0o M+Gu+KTyR0+ataX0VKSvFgc=WYgH -----END PGP SIGNATURE----- _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks! That''s great, Scott. I thought, "table cyclog has mood_id, therefore it ''has_one''". When in fact, it''s the other way around! Jamie On Jan 10, 2005, at 11:17 PM, Scott Barron wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Jamie Orchard-Hays wrote: > | I wonder if this is a typical newbie error: I had reversed the > concept > | of "has_one" and "belongs_to". I finally figured it out after playing > | with the models and looking at the log file. > | > | Thanks, > | > | Jamie > > I drew up a little diagram when I first started doing Rails, to keep > the > relationships straight in my head. If you haven''t come across it yet, > you can see it at http://curry.elitists.net/~scott/ARAssociations.png > > Enjoy! > - -Scott > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.2 (Darwin) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFB41NAvqYRQQGcpIQRAvUMAJ9drcvp24XhxaujHSvp4nM8UJ4i4wCfZA0o > M+Gu+KTyR0+ataX0VKSvFgc> =WYgH > -----END PGP SIGNATURE----- > <scott.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Scott Barron wrote:> > I drew up a little diagram when I first started doing Rails, to keep the > relationships straight in my head. If you haven''t come across it yet, > you can see it at http://curry.elitists.net/~scott/ARAssociations.pngI like that - what did you use to draw it?
On Fri, Jan 14, 2005 at 12:07:43PM -0600, Trevor wrote:> Scott Barron wrote: > > > > >I drew up a little diagram when I first started doing Rails, to keep the > >relationships straight in my head. If you haven''t come across it yet, > >you can see it at http://curry.elitists.net/~scott/ARAssociations.png > > > I like that - what did you use to draw it?Omnigraffle http://www.omnigroup.com/applications/omnigraffle/ Good stuff -Scott
> Omnigraffle http://www.omnigroup.com/applications/omnigraffle/ > > Good stuff > -ScottAwesome stuff comes to mind as well -- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog