Ruby on Rails Saturday, January 20, 2018

Thanks, Hassan ~

The link you provided produced some progress, and also some more perplexity.

From that article i learned that I had the association definitions in my model definitions backwards.  What I had as this:
class Offer < ApplicationRecord
  belongs_to
:person, class_name: "Worker", foreign_key: "worker_id"
end
should have been this:
class Offer < ApplicationRecord
  belongs_to
:worker, class_name: "Person", foreign_key: "worker_id"

In retrospect, that makes more sense:  The name of the association should not have to be the name of the associated class; if it had to be, we could not declare two semantically distinct associations between the same two classes.  The class_name parameter lets us declare the name of the associated class, so Rails doesn't have to infer it from the association name.  And the foreign_key parameter lets us be explicit about the column that refers to the associated class.

The good news is that, with that changed, I can get the display of an Offer (in index.html.erb and show.html.erb) to show the name of the Person, rather than just the object reference.  To do this, I set the relevant line in those files to read:
  <%= @offer.worker.name %>

The perplexing news is that it remains impossible to create or update an Offer.  When I try to change the Person who is associated with an existing Offer, the odd result is that no error is returned: the "show" page is displayed with the happy message "Offer was successfully updated."  But the associated Person is still the pre-existing one; the change was not saved.

And when I try to create a new Offer, I get an error that has appeared before:
1 error prohibited this offer from being saved:
Worker must exist

Both of these results say to me that the id of the Person that I'm trying to associate with this (existing or new) Offer is somehow not getting passed on to where it needs to go.

Further suggestions?

~ Tx, Ken

On Saturday, January 20, 2018 at 1:48:03 PM UTC-5, Hassan Schroeder wrote:
On Sat, Jan 20, 2018 at 10:16 AM, kenatsun <kena...@gmail.com> wrote:

> I added class_name: "Worker", foreign_key: "worker_id" to both Person and
> Offer, so the class definitions look like:
> class Person < ApplicationRecord
>     has_many :offers, class_name: "Worker", foreign_key: "worker_id"
> end
>
> class Offer < ApplicationRecord
>   belongs_to :person, class_name: "Worker", foreign_key: "worker_id"
> end
> but the forms still misbehave as described earlier.

Uh, well -- this will probably help:

http://guides.rubyonrails.org/association_basics.html#bi-directional-associations

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/61889a0d-6b20-4d2d-b5fe-46d6a6be93d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment