当我现在正在学习RoR时,我想知道一个更适当的(rails)方式来实现应用程序只显示相关资源。与select_name中的class_name相关的rails 4模型显示全部。一些建议需要
现在我有以下型号:
class Account < ActiveRecord::Base
has_many :billing_accounts
has_many :addresses
end
class BillingAccount < ActiveRecord::Base
belongs_to :invoice_address,
class_name: "Address",
foreign_key:"invoice_address_id"
end
class Address < ActiveRecord::Base
has_many :billing_accounts
belongs_to :account
end
在我edit.billing_account我有这样的形式:
= simple_form_for([:account, @billing_account]) do |f|
= f.association :invoice_address
我预计,只有相关联的地址将被shwon,但这个节目数据库中的“所有”地址记录(也来自其他用户帐户)。
用户只应该能够看到account.addresses
和现在我这样做有:
= f.association :invoice_address, collection: current_user.account.addresses.all
但我相信有更好的方式来做到这一点的机型里面。对于我现在使用的每种表格,我都会使用current_user.account.MODEL.all
,但这不是非常干燥。
所以基本上我想要的只是使用=f.association :invoice_address
而BillingAccount应该知道它只能显示account.addresses。
建议欢迎。谢谢!
嗨丘马科夫。感谢您的建议,但我不想编辑address_data。使用simple_fields我创建一个表单来编辑地址。但是我想将account.address关联到billing_account。 – Jerry 2014-10-10 21:58:26
我了解你。看到我的另一个答案。 – chumakoff 2014-10-10 22:24:59