Forum: Ruby on Rails Multiple "has" in ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a7fbbca80dcec9edc46bf97b8a9d10f?d=identicon&s=25 Jeff Lamarche (jlamarche)
on 2006-04-11 13:51
Please excuse a newbie question, but I couldn't find anything by
searching the archives.

What I want to know is how to have multiple references to the same
class/table within a class. For example, let's say I have a table called
People:

create table people (
    id     serial8 primary key
   ,name);

which just holds a list of the people in the database. Then I have
another table like this:

create table log (
   id     serial8 primary key
  ,created_by   int8 references people(id)
  ,created_for int8 references people(id)
);

Now, in my Model class, how do I handle this?

class Log < ActiveRecord::Base
  has_one   :People
end

Is there some way to differentiate the two different has_one references
but keep them looking at the same table?

Thanks much
Jeff
8d30b78dcd3ae8ff8d5e6085059060c7?d=identicon&s=25 Martin Bernd Schmeil (thebernd)
on 2006-04-11 14:03
class Log < ActiveRecord::Base
  belongs_to :crtd_by, :class_name => "People", :foreign_key =>
"created_by"
  belongs_to :crtd_for, :class_name => "People", :foreign_key =>
"created_for"
end

..or something along those lines.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-04-11 14:15
(Received via mailing list)
you are confusing has_one and belongs_to.  just remember that whatever
table
contains the foreign key is the one that is on the belongs_to side of
the
relationship.

are you sure you want has_one instead of has_many?  using has_one means
that
a person can only ever have at most one created_by log and one
created_for
log.

class Person < ActiveRecord::Base
  has_many :created_logs, :class_name => "Log", :foreign_key =>
"created_by"
# a person can create many logs
  has_many :assigned_logs, :class_name => "Log", :foreign_key =>
"created_for" # a person can have many logs created for them
end

# person.created_logs < -- all the logs that the person created
# person.assigned_logs < -- all the logs assigned (created_for) to that
person

class Log < ActiveRecord::Base
  belongs_to :created_by, :class_name => "Person", :foreign_key =>
"created_by"
  belongs_to :created_for, :class_name => "Person", :foreign_key =>
"created_for"
end

# log.created_by < -- person who created the log
# log.created_for < -- person the log is assigned to


feel free to rename the associations.  as long as you specify the class
name
and foreign key, you can name the association whatever you want.
4a7fbbca80dcec9edc46bf97b8a9d10f?d=identicon&s=25 Jeff Lamarche (jlamarche)
on 2006-04-11 14:19
Chris Hall wrote:
> are you sure you want has_one instead of has_many?  using has_one means
> that
> a person can only ever have at most one created_by log and one
> created_for
> log.

Well, this was just an example to illustrate the question. My actual
tables and classes have a lot of extraneous stuff in them that might
have gotten in the way of what I was asking.

Thanks much for the information! I appreciate the help.
Df040ca3576504b24a73744179903277?d=identicon&s=25 Tobias Lütke (Guest)
on 2006-04-11 14:49
(Received via mailing list)
Saving the planet from good variable names. One byte at a time.

On 4/11/06, Martin Bernd Schmeil <bschmeil@autoscout24.com> wrote:
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Tobi
http://shopify.com       - modern e-commerce software
http://typo.leetsoft.com - Open source weblog engine
http://blog.leetsoft.com - Technical weblog
8d30b78dcd3ae8ff8d5e6085059060c7?d=identicon&s=25 Martin Bernd Schmeil (thebernd)
on 2006-04-14 17:47
Tobias Lütke wrote:
> Saving the planet from good variable names. One byte at a time.
>
> On 4/11/06, Martin Bernd Schmeil <bschmeil@autoscout24.com> wrote:

Apologizes for the variable names in my example. It was close to my
lunch break and I couldn't complete my posting.

Since Jeff was using foreign key names without the id I just made sure
that the association names were not identical to the foreign key column
names. Otherwise Jeff might have run into trouble when trying to assign
an int to the variable like this

log.created_for = params[:created_for]

(Error: trying to assign Fixnum, expected People)

Since Jeff asked a "newbie question" I wanted to prevent him from
raising another "newbie problem" by using a rather bad variable name.

Again apologizes...
This topic is locked and can not be replied to.