Tuesday, 20 August 2013

store values in multiple table with circular dependency in rails

store values in multiple table with circular dependency in rails

I have created Three tables with attributes are following:
Table name: um_org_data , attributes: id, org_name, org_description,
webdomain
Table name: addresses , attributes: id, offc_addr, um_org_datum_id
Table name: phone_nos , attributes: offc_ph, offc_ext, address_id
Now I want that my organization with a name has multiple address means
Organization 1:M addresses and address has multiple telephone numbers
means address 1:M telephone_no
The code from my controllers are following:
um_org_datum.rb
class UmOrgDatum < ActiveRecord::Base
attr_accessible :org_description, :org_name, :webdomain, :addresses,
:addresses_attributes
has_many :addresses
accepts_nested_attributes_for :addresses
end
addresses.rb
class Address < ActiveRecord::Base
attr_accessible :offc_addr
belongs_to :um_org_datum
has_many :phone_nos
accepts_nested_attributes_for :phone_nos
end
phone_no.rb
class PhoneNo < ActiveRecord::Base
attr_accessible :offc_ph, :offc_ext
belongs_to :address
end
as association with organization and addresses is working fine... but
having problem in creating multiple telephone numbers.
the code for my view is as following:
<%= simple_nested_form_for(@um_org_datum) do |f| %>
<%= f.error_notification %>
<div class="container">
<div class="row">
<div class="span3 pull-right">
<div class="well">
<h2>Heading</h2>
<p>Sample text</p>
</div>
</div>
<div class="span9">
<%= simple_nested_form_for(@um_org_datum, :validate => true, :html
=> { :class => 'form-horizontal' }) do |f| %>
<div class="control-group">
<label class="control-label">Organization Name&nbsp;</label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :org_name, required: true, :autofocus =>
true %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Orgazination Description&nbsp;</label>
<div class="controls">
<%= f.text_area :org_description, :cols => "100", :rows =>
"10" %>
</div>
</div>
<div class="control-group">
<label class="control-label">Web Domain&nbsp;</label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :webdomain, required: true, :autofocus =>
true %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Office Address</label>
<div class="controls">
<%= f.link_to_add "<i class='icon-plus'></i>".html_safe,
:addresses%>
<%= f.fields_for :addresses do |task_form| %>
<div class="input-prepend">
<%= task_form.text_field :offc_addr, :label => false,
:placeholder => 'Office Address'%>
<div class="control-group">
<label class="control-label">Telephone</label>
<div class="controls">
<%= f.link_to_add "<i
class='icon-plus'></i>".html_safe, :phone_nos%>
<%= f.fields_for :phone_nos do |task_form| %>
<div class="input-prepend">
<%= task_form.text_field :offc_ph, :label =>
false, :placeholder => 'Office Address'%>
</div>
<div class="input-prepend">
<%= task_form.text_field :offc_ext, :label
=> false, :placeholder => 'Office
Extension'%>
</div>
<div class="input-prepend">
&nbsp;&nbsp;
<%= task_form.link_to_remove "<i
class='icon-remove'></i>".html_safe%>
<% end %>
</div>
</div>
<div class="input-prepend">
&nbsp;&nbsp;
<%= task_form.link_to_remove "<i
class='icon-remove'></i>".html_safe%>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label">Office Phone Number&nbsp;</label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :offc_ph, required: true, :autofocus =>
true %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<% if @um_org_datum.id == nil%>
<button class="btn btn-primary" type="submit">Submit</button>
<% else %>
<button class="btn btn-primary" type="submit">Update</button>
<% end %>
<a class="btn" href="/um_org_data"
style="text-color:black">Cancel</a>
</div>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
Now what i want to do is that i have three tables which are circular
dependent on each other.... organization table which has primary key that
uses in addresses table as foreign key and has own primary key which is
used in phone_nos table as foreign key.
I wants a form in which when click on + button under address show
telephone label with + button where user can add multiple telephones
against single address.. all is that.

No comments:

Post a Comment