How to register a new Person when you create a new Customer

Asked

Viewed 78 times

0

po I am with a question already a while breaking the head, next, I have a model Person, which is the Generica for person and the model Customer, which belongs to Person, so:

class Person < ActiveRecord::Base 
  has_one :customer
  has_one :address, as: :addressable
  has_one :phone, as: :phoneable


  accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank

  accepts_nested_attributes_for :phone, allow_destroy: true, reject_if: :all_blank

  accepts_nested_attributes_for :customer, allow_destroy: true, reject_if: :all_blank

end

and Customer:

class Customer < ActiveRecord::Base
  belongs_to :person
  has_one :address, as: :addressable
  has_one :phone, as: :phoneable

  validates :person_id, presence: true

  accepts_nested_attributes_for :phone, allow_destroy: true, reject_if: :all_blank

  accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :all_blank
  accepts_nested_attributes_for :person, allow_destroy: true, reject_if: :all_blank

end

In Customer_controller I set the Strong params to accept the attributes of Person when created, thus:

 def customer_params
   params.require(:customer).permit(
    :customer_code, :name, :cpf, :rg, :birthdate,
    addresses_attributes: [:id, :street, :number, :neighborhood, :state, :nation],
    person_attributes: [:id, :name, :cpf, :rg, :birthdate],
    phones_attributes: [:id, :phone1, :phone2])
  end

In the form I entered the simple_fields_for :person of |ff| But you are not registering the person, only the person.

If you can help.

2 answers

1

Speak Jean, all right?

On the face, we can see that we have double relationships, since we have:

has_one :address, as: :addressable
has_one :phone, as: :phoneable

in Person and also in Customer.

The fields of Person appear in the form? You may need a customer.build_person.

Another tip is to validate relationships without the _id, only validates :person, normally we validate the associations without the _id and use the _id for uniqueness validations.

Normally Rails checks the relationship when creating but you can make this explicit with the use of inverse_of in relations. More information here.

1


As dpedoneze commented check if you are instantiating the build_person object before starting fields_for

I would start this way in the view:

f.object.build_person if f.object.person.blank?
f.simple_fields_for :person do |ff| 

Another thing that’s getting me a little intrigued is Person’s relationship with Customer.

A person has a consumer? What is the responsibility of each entity?

Just one detail, are you using which version of Rails? If I’m not mistaken, in the version before 3.x the acceptd_nested did not work with belongs_to.


Replying to your reply in the comment:

I’ll give you a scenario to analyze there.

It would not be better to set a field in Person having person_type and merge the columns of Customer with person.

Because then you’d have something like this

Person.new(person_type:'Costumer') 

or in the future

Person.new(person_type: 'Company')
Person.new(person_type: 'Agent') 

or also use STI

The identification model is called User?

  • Hello Breno, thank you for the answer, just to answer your questions, a person has a consumer because a consumer is a person, and the Rails version is 4.2.6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.