2014-10-10 24 views
1

当我现在正在学习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。

建议欢迎。谢谢!

回答

0

在你的情况下,这里描述你应该使用f.simple_fields_for代替f.associationhttps://github.com/plataformatec/simple_form/wiki/Nested-Models

class BillingAccount < ActiveRecord::Base 
    belongs_to :invoice_address, 
      class_name: "Address", 
      foreign_key:"invoice_address_id" 
    accepts_nested_attributes_for :invoice_address 
end 

查看:

= simple_form_for([:account, @billing_account]) do |f| 
    = f.simple_fields_for :invoice_address do |f_address| 
    = f_address.input :street 
    = f_address.input :zipcode 
    ... 

不要忘记建立帐户的invoice_address在控制器如果需要。例如:

class BillingAccountController < ApplicationController 

    def new 
    @billing_account = BillingAccount.new 
    @billing_account.build_invoice_address 
    end 
+0

嗨丘马科夫。感谢您的建议,但我不想编辑address_data。使用simple_fields我创建一个表单来编辑地址。但是我想将account.address关联到billing_account。 – Jerry 2014-10-10 21:58:26

+0

我了解你。看到我的另一个答案。 – chumakoff 2014-10-10 22:24:59

0

你只需要设置default_scope嵌套模式:

class Address < ActiveRecord::Base 
    default_scope { where(account_id: current_user.account_id) } 

但在这种情况下,你应该define current_user in models

+0

HI yukke。我试过这个,但不知何故,我不断收到“未定义的局部变量或方法'current_user”错误。但那是我需要调查的另一件事。它确实给了我一个想法现在在哪里看。 – Jerry 2014-10-10 22:21:34

+0

@Jerry,仔细看看我的口水。有指向current_user钩子的链接。 – 2014-10-10 23:05:36

+0

我做到了。但是当我遵循这些解决方案时,我仍然遇到current_user错误。正如我om轨道4似乎有些事情不再工作了。 – Jerry 2014-10-11 06:55:22

0

由于您使用has_many可以用复数形式的型号名称而不是current_user.account.MODEL.all

像这样:

current_user.account.addresses 

current_user.account.billing_accounts 

它甚至其他方式与belongs_to

@address = Address.last 
@address.accounts 
+0

嗨Ecnalyr。 current_user.account.addresses正在工作,但我想摆脱** current_user.account.MODEL **的东西。必须有更好的方式来显示只有帐户相关的项目。 – Jerry 2014-10-10 22:11:42

+0

我相信只要使用= f.association:invoice_address,通过模型,应用程序就知道它只能使用account.addresses。但我还没弄明白。 – Jerry 2014-10-10 22:24:34

0

尝试添加条件belongs_to的关联关系:

class BillingAccount < ActiveRecord::Base 
    belongs_to :invoice_address, 
       ->(billing_account) { where "account_id = #{billing_account.account_id}" }, 
       class_name: "Address", 
       foreign_key:"invoice_address_id" 
end