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
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.