在分配给父模型期间将属性添加到连接模型

时间:2013-09-24 00:24:44

标签: sql ruby-on-rails ruby ruby-on-rails-3 activerecord

我有以下设置:

Schema.rb

ActiveRecord::Schema.define(version: 20130923235150) do

  create_table "addresses", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "user_addresses", force: true do |t|
    t.integer  "user_id"
    t.integer  "address_id"
    t.string   "purpose"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

User.rb:

class User < ActiveRecord::Base
  has_one :user_address
  has_one :primary_shipping_address, through: :user_address, class_name: :UserAddress, source: :address
  has_one :primary_billing_address,  through: :user_address, class_name: :UserAddress, source: :address
end

Address.rb:

class Address < ActiveRecord::Base
  has_one :user_address
  has_one :primary_shipping_user, through: :user_address, class_name: :UserAddress, source: :user
  has_one :primary_billing_user,  through: :user_address, class_name: :UserAddress, source: :user
end

UserAddress.rb:

class UserAddress < ActiveRecord::Base
  belongs_to :user
  belongs_to :address
end

当有人user.primary_billing_address = address时,我希望联接模型实例将“结算”设置为其目的。与运输和“运输”类似。实施例

irb(main):013:0> u = User.new
=> #<User id: nil, created_at: nil, updated_at: nil>
irb(main):014:0> a = Address.create
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):015:0> u.primary_billing_address = a
=> #<Address id: 3, created_at: "2013-09-24 00:13:07", updated_at: "2013-09-24 00:13:07">
irb(main):016:0> u.save!
=> true
irb(main):017:0> u.user_address
=> #<UserAddress id: 2, user_id: 3, address_id: 3, purpose: nil, created_at: "2013-09-24 00:13:18", updated_at: "2013-09-24 00:13:18">
(not what I want... purpose should be "billing")

我该怎么做才能使它适用于新的 AND 持久记录?我已经提出了90%的解决方案,但由于边缘情况我的方法没有抓住,因此打破了一些随机规范。

最棘手的部分是association=行为:在新记录上,它通过连接模型对关联进行排队。

PS:我遗漏了我用来获取我想要的地址的has_one关系的条件。我认为这个问题与此无关。

1 个答案:

答案 0 :(得分:1)

首先,关联有点偏离,primary_shipping_addressprimary_billing_address都会返回相同的地址。您可以将其更改为

class User < ActiveRecord::Base
  has_many :user_addresses # user can have multiple addresses one for shipping and one for billing
  has_one :primary_shipping_address,
           through: :user_address, class_name: :UserAddress, 
           source: :address, :conditions => ['user_addresses.purpose = ?','shipping']

  has_one :primary_billing_address, 
          through: :user_address, class_name: :UserAddress, 
          source: :address, :conditions => ['user_addresses.purpose = ?','billing']
end

要在保存地址时保存目的,有两个选项。

选项1:覆盖默认的association=方法

   # alias is needed to refer to original method
   alias_method :orig_primary_billing_address=, :primary_billing_address=
   def primary_billing_address=(obj)
     self.orig_primary_billing_address = obj
     self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, 'billing')  
   end

  # repeat for shipping

选项2:创建一个自定义方法(我更喜欢这个,因为它更清洁干爽)

   def save_address_with_purpose(obj,purpose)
      self.send("primary_#{purpose}_address=", obj)
      self.user_addresses.where("address_id = ?", obj.id).update_attribute(:purpose, purpose)  
   end
相关问题