Trouble using associations with to_param

When I do this I obtain nil:

current_shipment = current_entry.shipment

The models look like this:

class CaCustomsShipment < ActiveRecord::Base

has_one :ca_customs_entry
has_one :entry,
:class_name => ‘CaCustomsEntry’ #short form

def to_param
shipment_identifier
end

class CaCustomsEntry < ActiveRecord::Base

belongs_to :ca_customs_shipment
belongs_to :shipment, :class_name => ‘CaCustomsShipment’

def to_param
cctn
end

If I fetch the rows individually via .first then I see this:

  --- !ruby/object:CaCustomsEntry
  attributes:

. . .
created_at: 2011-02-22 16:40:49.085782
. . .
ca_customs_shipment_id: “111”

and

 --- !ruby/object:CaCustomsShipment
  attributes:

. . .
lock_version: “0”
id: “111”
. . .

However, If I do this:

puts( CaCustomsShipment.first.entry.to_yaml )
puts( CaCustomsEntry.first.shipment.to_yaml )

The first puts displays the entry I expect but the second returns nil. I
also discovered that this fails:

CaCustomsShipment( CaCustomsEntry.first.ca_customs_shipment_id )

but this succeeds

CaCustomsShipment.find_by_id(
CaCustomsEntry.first.ca_customs_shipment_id )

This gave rise to the thought that perhaps I was handling the to_param
methods incorrectly. I tried removing the to_param definitions from the
respective models but the program behaviour does not appear to change.

Clearly I am missing something or other in the association method call
but I cannot seem to find what that something is.

Does anyone here see what I am doing wrong?

James B. wrote in post #983156:

When I do this I obtain nil:

current_shipment = current_entry.shipment

. . .

Clearly I am missing something or other in the association method call
but I cannot seem to find what that something is.

Does anyone here see what I am doing wrong?

What I did was to change the association call named #shipment to:

current_shipment = current_entry.ca_customs_shipment

and things worked. So, now my question is: what is wrong with this
code?

#shipment returns nil always

belongs_to :shipment, :class_name => ‘CaCustomsShipment’

returns ca_customs_shipment

belongs_to :ca_customs_shipment

On 22 February 2011 18:34, James B. [email protected] wrote:

Does anyone here see what I am doing wrong?

What I did was to change the association call named #shipment to:

current_shipment = current_entry.ca_customs_shipment

and things worked. So, now my question is: what is wrong with this
code?

#shipment returns nil always

belongs_to :shipment, :class_name => ‘CaCustomsShipment’

Perhaps you need :foreign_key here also.

Colin

On 22 February 2011 21:23, James B. [email protected] wrote:

example of this usage early up in the introduction where I think it
might be very helpful.

Sometimes you need it on the has_many association also. So if
Foo has_many :bars
and the key in bars is not foo_id then you will need
Foo has_many :bars, :foreign_key => ‘whatever_id’

In your case if the key is ca_customs_shipment_id then you should not
need it on the has_many.

Colin

Colin L. wrote in post #983199:

Perhaps you need :foreign_key here also.

Colin

I will try this if I ever go back to that idiom. Based on your
suggestion I discover that this parameter is required only for the
belongs_to method. The API documentation does not give an explicit
example of this usage early up in the introduction where I think it
might be very helpful.