Rails 3- Nested Objects (Reusing the Address Model)

I am having problems with this question.

I have a business model and an address model. To complicate the situation, I also have a location model. A business can have multiple locations, but only one mailing address.

Here is a simple diagram of objects.

Business
-Name
-MailingAddress

Location
-Business
-Name
-Address

I want to reuse the address model for both businesses and locations. What do I need to do for this. If this helps, I use MySQL.

First, how do I structure models? Do I need has_many: location ,: business (for Location / Business)? Then how can I nest the forms for creating addresses in the new Business / Location forms.

Thank!

+3
2

, , .

:

#app/models/business.rb
class Business < ActiveRecord::Base
  has_one :mailing_address, :class_name => "Address", :dependent => :destroy
  accepts_nested_attributes_for :mailing_address

  has_many :locations
  accepts_nested_attributes_for :locations
end

#app/models/location.rb
class Location < ActiveRecord::Base
  has_one :address, :dependent => :destroy
  accepts_nested_attributes_for :address

  belongs_to :business
end

#app/models/address.rb
class Address < ActiveRecord::Base
  belongs_to :business
  belongs_to :location
end

:

#app/controllers/businesses_controller.rb#new
def new
  @business = Business.new

  mailing_address = @business.build_mailing_address
  3.times do
    location = @business.locations.build
    address = location.build_address
  end
  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @business }
  end
end

, :

#app/views/businesses/_form.html.erb
<%= form_for(@business) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :mailing_address do |builder| %>
    <h3>Mailing Address</h3>
    <%= render "address_fields", :f => builder %>
  <% end %>
  <%= f.fields_for :locations do |builder| %>
    <h3>Location</h3>
    <%= render "locations_fields", :f => builder %>
    <%= builder.fields_for :address do |mini_builder| %>
      <h3>Location Address</h3>
      <%= render "address_fields", :f => mini_builder %>
    <% end%>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

:

#app/views/businesses/_address_fields.html.erb
<p>
  <%= f.label "Street Address"%>
  <%= f.text_field :street %>
  <%= f.label "City"%>
  <%= f.text_field :city %>
  <%= f.label "State"%>
  <%= f.text_field :state %>
  <%= f.label "Zip"%>
  <%= f.text_field :zip %>  
</p>

:

#app/views/businesses/_location_fields.html.erb
<p>
  <%= f.label "Location Name"%>
  <%= f.text_field :name%>
</p>

. , has_one has_many - . , .

- , , -, - :

#app/models/business.rb#possible_mailing_addresses
def possible_mailing_addresses
  Location.where(:business_id=>self.id).joins(:address)
end
+8

, , , - :

class Business < ActiveRecord::Base
  has_many :locations
  has_many :addresses, :through => :locations do
    def mailing
      find_by_is_mailing_address(true)
    end
  end
end

class Location < ActiveRecord::Base
  has_one :address
  belongs_to :business
end

class Address < ActiveRecord::Base
  belongs_to :location
  belongs_to :business, :through => :location      
end

... my_business.addresses.mailing.

addresses is_mailing_address, true , . Address, is_mailing_address.

, ; , - .

Ruby. , Business has_one :mailing_address :through => :locations :source, , :source. , ?

0

Source: https://habr.com/ru/post/1774501/


All Articles