[Rails] newbie: has_one? composed_of?
Bill Atkins
batkins57 at gmail.com
Mon Jan 10 16:32:45 GMT 2005
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 at gmail.com> 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 at dang.com> wrote:
> > asking for help again...
> >
> >
> > ----- Original Message -----
> > From: "Jamie Orchard-Hays" <jamie at dang.com>
> > To: <rails at lists.rubyonrails.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 at lists.rubyonrails.org
> > > http://lists.rubyonrails.org/mailman/listinfo/rails
> > _______________________________________________
> > Rails mailing list
> > Rails at lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.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"
More information about the Rails
mailing list