seixasfelipe / cleaners

Cleaners - Management Application
MIT License
2 stars 0 forks source link

Address CRUD #37

Closed fabianoalmeida closed 11 years ago

fabianoalmeida commented 11 years ago

This model could be an unplugged model and all the other models that belongs to the address model will create a foreign key to this model. Fo example, I'm a customer and I have an address.

So, it's necessary update the customer table with a foreign key to address table

class AddAddressToCustomer < ActiveRecord::Migration
  def change
    add_column :customers, :address_id, :integer
  end
end

And in the customer model define the relationship between these models. A little ruby code to demonstrate my idea:

# app/models/customer.rb
class Customer < ActiveRecord::Base
  # ...
  belongs_to :address
  accepts_nested_attributes_for :address, allow_destroy: true
end

And here is an example of the address model:

# app/models/address.rb
class Address < ActiveRecord::Base

  attr_accessible :country, :country_id, :state, :state_id, :street, 
                  :district, :city, :zip_code

  belongs_to :country # if this is a model
  belongs_to :state   # if this is a model
end

None attribute will be mandatory on address model. If the customer inform his address we will fill all the fields and persist them in database.

What do you think?

fabianoalmeida commented 11 years ago

I created this issue after suggested by @seixasfelipe here on #16.

fraga commented 11 years ago

'customer' have zero or one 'address', which attribute active_record has to fulfill this requirement?

fabianoalmeida commented 11 years ago

As address model is unplugged we can create a partial form with all address fields and add it on the customer partial form. For example:

# app/views/customers/_form.html.erb
<%= simple_form_for( @customer ) do |f| %>
    <%= f.simple_fields_for :address do |a| %>
      <%= render 'address_fields', f: a %>
    <% end %>
    <%= f.button :submit, :class => 'large primary' %>
<% end %>

In this example the partial form of address is address_fields and I'm telling to simple_form that exists a model (f.simple_fields_for) inside the customer called address that I want to fill using this current form.

So using this solution we can guarantee that a customer have none, because all address fields isn't required, or one address. :wink: In this case, rails will create a new register of address with empty values if the fields were not filled.

Is it more clear now? :neutral_face:

fraga commented 11 years ago

Nice! Yeah it's clear! Thanks!

fabianoalmeida commented 11 years ago

Great! What do you think about my ideas to address model? Can I go ahead with it? Can I create State and Country models? I need a simple feedback to start with this.

fraga commented 11 years ago

Yeah go for it

fraga commented 11 years ago

@seixasfelipe ??

seixasfelipe commented 11 years ago

Go Go Go! =)

On Thu, Apr 11, 2013 at 7:34 PM, Rodrigo Fraga notifications@github.comwrote:

@seixasfelipe https://github.com/seixasfelipe ??

— Reply to this email directly or view it on GitHubhttps://github.com/seixasfelipe/cleaners-panamby/issues/37#issuecomment-16268219 .