Ruby on Rails Saturday, January 20, 2018



On Saturday, January 20, 2018 at 10:49:50 AM UTC-5, Walter Lee Davis wrote:

> On Jan 19, 2018, at 7:37 PM, fugee ohu <fuge...@gmail.com> wrote:
>
>  can create person objects with Adress.....person.new but if I'm creating a new person object from @person from a form, how do i create the entry to the relationships table since I don't have @person.id yet
>
>

You don't need to do that -- Rails cleans up those values for you when it saves the parent. So if you have

@user = User.new(user_params)
@widgets = @user.widgets.build(widget_params)

When you save @user, the @widgets will be saved as well, and the correct ID will be put into each of the @widgets.

Walter

i was actually able to get @person.id after if  @person.save Relationship.find_or_create_by(person_id: @person.id ...

--
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/614c7802-c2b9-4e03-b276-3f1c857bed02%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

is multitenancy only for apps that have users?

--
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/d6a41c52-badd-41ba-8d86-e47072611f0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

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.