has_one example

335 views
Skip to first unread message

sohdubom

unread,
Jan 9, 2009, 2:54:10 PM1/9/09
to Ruby or Rails Oceania
as an example i thought that the following case could be a good one
for has_one relationship, but i ended up very confused:

- suppose i have a Comment model with attributes: author, content,
status (just 3 to simplify)

- i wouldn't do that in a real scenario, but let's say i don't want to
create enums for status and decide to normalize it by creating an
extra model: Status

in this case: a comment has_one status (like new, ok, bad, ...), so
the Comment model will have as fk: status_id and status belongs_to
comment (in singular) or status belongs_to comments (plural) ?

Matt

unread,
Jan 9, 2009, 8:11:07 PM1/9/09
to rails-...@googlegroups.com
That's not a great case for the has_one association because as you pointed out, the real world usage probably isn't there.

Here's a couple for you

class Student < ActiveRecord::Base
  # so in here, there's an id, name for the student and any other details they may have
  has_one :locker
end

class Locker < ActiveRecord::Base
  # here you have the fields id, locker_number, student_id (this is the one for the association)
  belongs_to :student
end

---

class Sale < ActiveRecord::Base
  # id and whatever other fields
  has_one :invoice
end

class Invoice < ActiveRecord::Base
  # id, sale_id, any other fields you want
  belongs_to :sale
end

Do they help at all?

Matt Didcoe
mat...@gmail.com

Cameron Barrie

unread,
Jan 12, 2009, 12:51:04 AM1/12/09
to rails-...@googlegroups.com
Models would actually be so.

class Post < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base

belongs_to :status
belongs_to :post

end

class Status < ActiveRecord::Base

has_many :comments

end

Simple one to many relationships.
gives you following instance methods
Post#comments
Comment#post
Comment#status
Status#comments

Does that make sense?


Cam

Ryan Bigg

unread,
Jan 12, 2009, 1:06:31 AM1/12/09
to rails-...@googlegroups.com
A status in your example would "has_many" comments, as many comments can have the same status. Therefore, a comment would belongs_to a status.
-----
Ryan Bigg
Freelancer






Ben Hoskings

unread,
Jan 12, 2009, 1:41:41 AM1/12/09
to rails-...@googlegroups.com
Yep.

If it's a 1-n relationship (as opposed to an n-n), the model with the foreign key is the one that belongs_to the other model.

Ben Hoskings

Clifford Heath

unread,
Jan 12, 2009, 1:53:03 AM1/12/09
to rails-...@googlegroups.com
In ActiveFacts you have a one_to_one (sorta what has_one is, but
symmetrical,
you just say it on one end and you get the other end for free), and
has_one,
which is sort what belongs_to is, except it also creates the symmetric
has_many
on the other model.

You never need to say has_many, it comes for free.

Clifford Heath.

Ben Hoskings

unread,
Jan 12, 2009, 3:06:46 AM1/12/09
to rails-...@googlegroups.com
That's pretty nice. It seems like a much neater idea to always just
define it on the model with the foreign key, because after all, the
has_many is really just the same relationship, implied in the other
direction.

Ben Hoskings

sohdubom

unread,
Jan 12, 2009, 7:19:29 AM1/12/09
to Ruby or Rails Oceania
hi thanks for the tips :-)
Reply all
Reply to author
Forward
0 new messages