Multiple "has" in ActiveRecord


#1

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


#2

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.


#3

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.


#4

Chris H. 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.


#5

Saving the planet from good variable names. One byte at a time.

On 4/11/06, Martin Bernd S. removed_email_address@domain.invalid wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
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


#6

Tobias Lütke wrote:

Saving the planet from good variable names. One byte at a time.

On 4/11/06, Martin Bernd S. removed_email_address@domain.invalid 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…