Ruby on rails 当模型有多个时,通过模型有一个吗

Ruby on rails 当模型有多个时,通过模型有一个吗,ruby-on-rails,ruby-on-rails-3,associations,relationship,Ruby On Rails,Ruby On Rails 3,Associations,Relationship,我正在尝试建立一个系统,在这个系统中,我的账单档案可以通过用户保存地址和付款方式。对于地址,它可以正常工作,因为用户只有一个地址,但是对于付款方式,因为用户有很多地址,我不断得到错误: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :payment_method_id in model User. Try 'has_many :payme

我正在尝试建立一个系统,在这个系统中,我的账单档案可以通过用户保存地址和付款方式。对于地址,它可以正常工作,因为用户只有一个地址,但是对于付款方式,因为用户有很多地址,我不断得到错误:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: 
Could not find the source association(s) :payment_method_id in model User. 
Try 'has_many :payment_method, :through => :user, :source => <name>'. 
Is it one of :address, :billing_profile, or :payment_methods?**
你知道这是否可能,或者有更好的方法吗?我曾经尝试过在创建账单配置文件时手动设置id的想法,但随后我必须创建方法来获得支付方法,这并不可怕,但如果Rails能够为我做到这一点,那就太好了

编辑

所以既然我所希望的似乎是不可能的。我简单地在账单配置文件中添加了一个方法来模拟关联

def payment_method
    PaymentMethod.find(payment_method_id)
  end 

:至
选项用于链接关系。因此,以下观点是正确的:

billing_profile.user.address == biling_profile.address
但是,以下情况不可能是真的(因为您只希望一侧有一个列表,另一侧有一个列表):

您需要一个额外的列来建立此关系。如果两者都返回相同的值,则只能使用
:through
关系,因为没有“额外内存”来只保存列表中的一个


因此,简而言之,您需要添加一个
有一个
或一个
属于
而没有
:trhough
,并添加一个新列(额外内存)来保存此关系。

您需要使用
有一个:支付方法
。为了保持一致性(付款方式应与用户相同),您可以使用自定义验证方法。这实际上不起作用,因为当您对其运行查询时,它会在PaymentMethods表中查找不存在的billing_profile_id。我想在不增加专栏的情况下加入协会。如果还不清楚,请问我,我可以试着多写一点,但现在我找不到更好的方式来解释。唉,这就是我担心的。我希望有一些隐藏的rails魔法:P@justNeph对不起,今天没时间了:-)
class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true

  attr_accessible :city, :country, :state, :street_line_1, :street_line_2, :zip_code
end
class PaymentMethod < ActiveRecord::Base
  attr_accessible :user_id, :is_default

  belongs_to :user

  validates_presence_of :user_id
end
create_table :billing_profiles do |t|
  t.integer :user_id
  t.integer :payment_method_id
  t.timestamps
 end
def payment_method
    PaymentMethod.find(payment_method_id)
  end 
billing_profile.user.address == biling_profile.address
billing_profile.user.payment_methods == billing_profile.payment_method