rails generate scaffold Person name:string
rails generate scaffold Offer terms:string person:belongs_to
rake db:migrate
...
<% @offers.each do |offer| %>
<tr>
<td><%= offer.terms %></td>
<td><%= offer.person.name %></td>
<td><%= link_to 'Show', offer %></td>
<td><%= link_to 'Edit', edit_offer_path(offer) %></td>
<td><%= link_to 'Destroy', offer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
...
and the tweaked Offer _form.html.erb looked like this:<%= form_with(model: offer, local: true) do |form| %>
...
<div class="field">
<%= form.label :terms %>
<%= form.text_field :terms, id: :offer_terms %>
</div>
<div class="field">
<%= form.label :person_id %>
<%= form.collection_select :person_id, Person.order(:name), :id, :name %>
</div>
...
class RenameOfferPersonId < ActiveRecord::Migration[5.1]
def change
rename_column :offers :person_id :worker_id
end
end
Showing /home/ec2-user/environment/ww1/app/views/offers/index.html.erb where line #18 raised:
undefined method `name' for nil:NilClass
...
<td><%= offer.person.name %></td>
<td><%= offer.worker_id %></td>
ActiveModel::UnknownAttributeError in OffersController#update
unknown attribute 'person_id' for Offer.
Extracted source (around line #44):
...
44 if @offer.update(offer_params)
<%= form.label :person_id %>
<%= form.collection_select :person_id, Person.order(:name), :id, :name %>
<%= form.label :worker_id %>
<%= form.collection_select :worker_id, Person.order(:name), :id, :name %>
1 error prohibited this offer from being saved:- Person must exist
Using scaffold, I generated two models, Person and Offer, and the forms to manage them. Offer has a belong_to association with Person. After running the generator, all the forms immediately worked fine.
But then I discovered an external requirement that the foreign key column in the offers table, which was named person_id per Rails conventions, needed to be renamed as worker_id. Since then, the Offer forms don't work. They evoke a variety of errors, all related to the renamed column. It seems that the system no longer recognizes it as implementing the Offer to Person association.
I figure I need to make some declaration somewhere to restore this associative functionality, but I can't figure out how to do this. Can you help?
belongs_to (and other assocns) has options for class name and foreign key. see ri or doc.otoh, i wonder if it would have been easier to create just another Worker resource (and probly delete Person); but ymmv prolly.best regards --botp
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
class Offer < ApplicationRecord
belongs_to :person, class_name: "Worker", foreign_key: "worker_id"
end
class Offer < ApplicationRecord
belongs_to :worker, class_name: "Person", foreign_key: "worker_id"
<%= @offer.worker.name %>
1 error prohibited this offer from being saved:
Worker must exist