Syntax q on ActiveRecord

In my model, if I have a field, that contains the id to another table,
twice
(like, primary associate and a secondary associate) I am specifying this
as

class Customer < ActiveRecord::Base
belongs_to: associate :foreign_key => “associatekey1”
belongs_to: associate :foreign_key => “associatekey2”
end

This doesn’t work – but my question is how SHOULD the syntax for
specifying
this look? Thanks, Ike

On Oct 31, 2006, at 11:00 AM, Ike wrote:

In my model, if I have a field, that contains the id to another
table, twice
(like, primary associate and a secondary associate) I am specifying
this as

this question should go to the rubyonrails mailing list.

Ike [email protected] wrote:

In my model, if I have a field, that contains the id to another table, twice
(like, primary associate and a secondary associate) I am specifying this as

class Customer < ActiveRecord::Base
belongs_to: associate :foreign_key => “associatekey1”
belongs_to: associate :foreign_key => “associatekey2”
end

This doesn’t work – but my question is how SHOULD the syntax for specifying
this look?

Just RTFM.

http://api.rubyonrails.org/

(s.v. "belongs_to):

belongs_to :firm, :foreign_key => “client_of”

Notice the comma? Notice where the colons go?

m.

PS The fact that you could bring yourself to write belongs_to: suggests
that you don’t know Ruby. Over the course of a long life in computers I
have found that it is helpful to know the basics of the language in
which one is programming (though, to be quite honest, I’ve written quite
a bit of Perl without obeying that rule)…

“matt neuburg” [email protected] wrote in message
news:[email protected]

Notice the comma? Notice where the colons go?
matt neuburg, phd = [email protected], Matt Neuburg’s Home Page
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It’s free and smart. http://www.tidbits.com

Jesus H. Christ…it’s a TYPO as I was copying by hand, with from the
code
on another machine, without considering that. Thanks for being so
chastising. -Ike

Ike wrote:

You probably could have looked it up, and it should go on the rails
list. However, people don’t need to be rude. RTFM should be banned
from this list, plus the rails documentation is scattered and it sorta
sucks.

Using the column names primary_associate_id and secondary_associate_id,
instead of associatekey1 and associatekey2

class Customer < ActiveRecord::Base
belongs_to :primary_associate, :class_name => ‘Associate’,
:foreign_key => ‘primary_associate_id’
belongs_to :secondary_associate, :class_name => ‘Associate’,
:foreign_key => ‘secondary_associate_id’
end

Rails automatically infers the class_name and foreign_key from the name
of your association. You can always just name your association whatever
you want and specify those things explicitly, like I do here.

matt neuburg wrote:

m.

PS The fact that you could bring yourself to write belongs_to: suggests
that you don’t know Ruby. Over the course of a long life in computers I
have found that it is helpful to know the basics of the language in
which one is programming (though, to be quite honest, I’ve written quite
a bit of Perl without obeying that rule)…

Hah, that post was so Tom Christiansen like, 10 years ago, on the Perl
mailing list. Brings back memories.

Outside of the silly attitude, I’d just like to point out that the
placement of the colons in Ruby is very odd compared to other popular
programming languages out there. Using a :colon like a $sigil takes some
getting used to, and assuming “colon: value” is much more consistent
with JSON/YAML/etc.

Point being, that’s an easy mistake to make.

-Nate

“Mike H.” [email protected] wrote in message
news:[email protected]

Ike wrote:

Rails automatically infers the class_name and foreign_key from the name of
your association. You can always just name your association whatever you
want and specify those things explicitly, like I do here.

Thanks Mike,

I’m stuck with legacy DB column names here however – I think maybe I
need
to look deeper into the ActiveRecord source. Thanks for your help. -Ike

Ike [email protected] wrote:

Jesus H. Christ…it’s a TYPO

Sorry, I must have misunderstand the question. I apologize. m.

On Oct 31, 2006, at 12:04 PM, Mike H. wrote:

You probably could have looked it up, and it should go on the rails
list. However, people don’t need to be rude. RTFM should be
banned from this list, plus the rails documentation is scattered
and it sorta sucks.

BZZZZZT. This is the ruby-talk mailing list, not the rails mailing
list. Take your chastisement elsewhere.

Ryan D. wrote:

This is also a newsgroup and I haven’t noticed any news groups for
rails. While you might not like people asking questions about one of
the programs using Ruby on your precious mailing list not everyone is as
close-minded as you seem to be.
I only found out about the mailing list after finding the newsgroup and
have found no reason to use the mailing list. I am sure that I am not
the only one.

From: “Ryan D.” [email protected]

On Oct 31, 2006, at 12:04 PM, Mike H. wrote:

You probably could have looked it up, and it should go on the rails
list. However, people don’t need to be rude. RTFM should be
banned from this list, plus the rails documentation is scattered
and it sorta sucks.

BZZZZZT. This is the ruby-talk mailing list, not the rails mailing
list. Take your chastisement elsewhere.

Rails-schmails. What happened to, “Matz is nice, so we are nice?”

Regards,

Bill

P.S. … YOUR MAMA !!!

Michael W. Ryder wrote:

list. Take your chastisement elsewhere.

This is also a newsgroup and I haven’t noticed any news groups for
rails. While you might not like people asking questions about one of
the programs using Ruby on your precious mailing list not everyone is as
close-minded as you seem to be.
I only found out about the mailing list after finding the newsgroup and
have found no reason to use the mailing list. I am sure that I am not
the only one.

Wow, this is getting almost as exciting as C++ land.

Ryan D. wrote:

list. Take your chastisement elsewhere.

Ryan, you weren’t the rude one, saying “go to the rails mailing list”
was both accurate and polite. The rudeness was the other guy. I stand
by what I said, saying RTFM is cursing at a stranger, and the question
wasn’t overly rudimentary.

Ike wrote:

Using the column names primary_associate_id and secondary_associate_id,
your association. You can always just name your association whatever you

I just changed the names of the columns in my example cause I felt like
it. This is perfectly fine as well

class Customer < ActiveRecord::Base
belongs_to :primary_associate, :class_name => ‘Associate’, :foreign_key
=> ‘associatekey1’
belongs_to :secondary_associate, :class_name => ‘Associate’,
:foreign_key => ‘associatekey2’
end

The :foreign_key option tells it the name of the column you are using.

Have you looked at the manual that was linked earlier in the thread?

http://api.rubyonrails.com/

Here’s the specific page for this

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000532

The documentation isn’t great, but for most problems at this level it
tells you what you need to know.