2013-07-15 40 views
0

我是新手创建我的第一个应用程序,一些红宝石爱好者告诉我,我的方法编码哪里效率不高。因为进入一个干净的状态需要很多工作,所以我想确保为我做的更好。Rails最佳实践 - 我可以使用很多“有很多”吗?

在下面的代码中,您是否认为我在过度使用has_many方法,如果是,应该如何替换它?

class Product < ActiveRecord::Base 

    # Offerers 
    has_many :ownerships, dependent: :destroy 
    has_many :owners, through: :ownerships, 
         source: :offerer 

    # Seekers 
    has_many :loans, dependent: :destroy 
    has_many :borrowers, through: :loans, 
        source: :seeker 



    # Owners 
    has_many :previous_ownerships, -> { where 'owning_date IS NOT NULL AND giving_date IS NOT NULL', agreed: true }, 
           class_name: 'Ownership' 
    has_one :current_ownership, -> { where current: true, agreed: true }, 
          class_name: 'Ownership' 
    has_many :next_ownerships, -> { where owning_date: nil, giving_date: nil }, 
          class_name: 'Ownership' 

    has_many :previous_owners, through: :previous_ownerships, 
          source: :offerer 
    has_one :owner, through: :current_ownership, 
        source: :offerer 
    has_many :next_owners, through: :next_ownerships, 
         source: :offerer 


    # Borrowers 
    has_many :previous_loans, -> { where 'return_date IS NOT NULL AND borrowing_date IS NOT NULL', agreed: true }, 
          class_name: 'Loan' 
    has_one :current_loan, -> { where current: true, agreed: true }, 
           class_name: 'Loan' 
    has_many :next_loans, -> { where borrowing_date: nil, return_date: nil }, 
            class_name: 'Loan' 

    has_many :previous_borrowers, through: :previous_loans, 
           source: :seeker 
    has_many :next_borrowers, through: :next_loans, 
          source: :seeker 
    has_one :borrower, through: :current_loan, 
           source: :seeker 

# Agreed or refused owners 
    has_many :agreed_ownerships, -> { where agreed: true, owning_date: nil, giving_date: nil }, 
           class_name: 'Ownership' 
    has_many :possible_ownerships, -> { where agreed: nil, owning_date: nil, giving_date: nil }, 
           class_name: 'Ownership' 
    has_many :refused_ownerships, -> { where agreed: false, owning_date: nil, giving_date: nil }, 
           class_name: 'Ownership' 
    has_many :agreed_owners, through: :agreed_ownerships, 
          source: :offerer 
    has_many :possible_owners, through: :possible_ownerships, 
          source: :offerer 
    has_many :refused_owners, through: :refused_ownerships, 
          source: :offerer 

    # Agreed or refused borrowers 
    has_many :agreed_loans, -> { where agreed: true, borrowing_date: nil, return_date: nil }, 
           class_name: 'Loan' 
    has_many :possible_loans, -> { where agreed: nil, borrowing_date: nil, return_date: nil }, 
           class_name: 'Loan' 
    has_many :refused_loans, -> { where agreed: false, borrowing_date: nil, return_date: nil }, 
           class_name: 'Loan' 
    has_many :agreed_borrowers, through: :agreed_loans, 
          source: :seeker 
    has_many :possible_borrowers, through: :possible_loans, 
          source: :seeker 
    has_many :refused_borrowers, through: :refused_loans, 
          source: :seeker 

回答

1

在许多地方,您使用has_many就好像它是一个范围。

阅读范围here

作用域允许您定义可以像方法一样访问的查询。例如:

class Loan < ActiveRecord::Base 
    scope :current, where(current: true, agreed: true) 
end 

可称为像这样:Product.first.loans.current

确保你把这些范围在合适的型号太多,你不希望在例如产品型号来筛选贷款。

+0

谢谢!如果我想获得我的产品的关联用户,我该怎么办? Product.first.loans.current.borrowers是否可以工作? –

+0

只要您的所有关系和范围都以良好的导轨方式定义,就应该这样做。搏一搏! – Matt

+0

不,它不起作用...'Product.first.loans.current'发送一个'loan'数组,因此我不能使用仅适用于一个对象的链式方法... –