Custom db entry for 3 way habtm in ROR

孤人 提交于 2019-12-19 03:59:29

问题


I am converting an existing perl gtk app into a ROR app

I have a 3 way habtm association model.

class CustomerMaster < ActiveRecord::Base
  has_and_belongs_to_many :address_master, :join_table => "customer_phone_addres"
  has_and_belongs_to_many :phone_master, :join_table => "customer_phone_address"

class AddressMaster < ActiveRecord::Base
    has_and_belongs_to_many :customer_master, :join_table => "customer_phone_addres"
    has_and_belongs_to_many :phone_master, :join_table => "customer_phone_addres"

class PhoneMaster < ActiveRecord::Base
  has_and_belongs_to_many :customer_master, :join_table => "customer_phone_addres"
  has_and_belongs_to_many :address_master, :join_table => "customer_phone_addres"

The join table has the following schema

CREATE TABLE customer_phone_address
(
  id bigserial NOT NULL,
  customer_master_id bigint,
  phone_master_id bigint,
  address_master_id bigint,
  CONSTRAINT customer_phone_address_pkey PRIMARY KEY (id),
  CONSTRAINT address FOREIGN KEY (address_master_id)
      REFERENCES address_master (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT customer FOREIGN KEY (customer_master_id)
      REFERENCES customer_master (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT phone FOREIGN KEY (phone_master_id)
      REFERENCES phone_master (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT uniq_cust_phone_address UNIQUE (customer_master_id, phone_master_id, address_master_id)
)

And I have created a nested form for customer_master#new which takes inputs for both address_master and phone_master

Originally for the perl gtk app, only one entry is created in the join_table for each entry of customer, address and phone

id|customer_master_id|phone_master_id|address_master_id
186767|182774|500773|210683

However using the above relationship model, I get two entries in case of ROR

id|customer_master_id|phone_master_id|address_master_id
186769|182810|500775|nil|
186770|182810|nil|211935|

I need to maintain backward compatibility to the perl gtk app. How do I get that single entry in the join table instead of the two entries?

I think this question is similar to https://stackoverflow.com/questions/5507150/custom-join-3-tables-usage-in-rails-2-3-8


回答1:


Assume you are using Rails 3.

You should use habtm when your join table has only 2 foreign keys. For most cases, has_many :through will be more flexible.

In your case, you should create a model for the join table. First, you should disable pluralization for legacy DB schema. (I thought you already did that).

# In config/application.rb
config.active_record.pluralize_table_names = false

Create a join table, namely CustomerPhoneAddress by convention (I will give it a more meaningful name):

# customer_phone_address.rb
class CustomerPhoneAddress < ActiveRecord::Base
  belongs_to :address_master
  belongs_to :customer_master
  belongs_to :phone_master
end

Finally, associate your models with has_many and has_many :through:

# address_master.rb
class AddressMaster < ActiveRecord::Base
  has_many :customer_phone_addresses
  has_many :customer_masters, :through => :customer_phone_addresses
  has_many :phone_masters, :through => :customer_phone_addresses
end

# customer_master.rb
class CustomerMaster < ActiveRecord::Base
  has_many :customer_phone_addresses
  has_many :address_masters, :through => :customer_phone_addresses
  has_many :phone_masters, :through => :customer_phone_addresses
end

# phone_master.rb
class PhoneMaster < ActiveRecord::Base
  has_many :customer_phone_addresses
  has_many :customer_masters, :through => :customer_phone_addresses
  has_many :phone_masters, :through => :customer_phone_addresses
end

I tested the code and it works very well. To create an association:

CustomerPhoneAddress.create(
  :phone_master => PhoneMaster.first,
  :address_master => AddressMaster.first,
  :customer_master => CustomerMaster.first)

To query the association:

IRB> a = AddressMaster.first
=> #<AddressMaster id: 1, created_at: "2011-12-07 15:23:07", updated_at: "2011-12-07 15:23:07">
IRB> a.customer_masters
=> [#<CustomerMaster id: 1, created_at: "2011-12-07 15:23:15", updated_at: "2011-12-07 15:23:15">]
IRB> a.phone_masters
=> [#<PhoneMaster id: 1, created_at: "2011-12-07 15:23:19", updated_at: "2011-12-07 15:23:19">]
IRB> a.customer_phone_addresses
=> [#<CustomerPhoneAddress id: 1, address_master_id: 1, customer_master_id: 1, phone_master_id: 1, created_at: "2011-12-07 15:24:01", updated_at: "2011-12-07 15:24:01">]


来源:https://stackoverflow.com/questions/8375131/custom-db-entry-for-3-way-habtm-in-ror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!