i'm newbie creating first app, , ruby lovers told me ways of coding not efficients. because it's lot of work come clean state, want sure better me.
in following code, think i'm overusing has_many
method, , if so, how should replace ?
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, -> { 'owning_date not null , giving_date not null', agreed: true }, class_name: 'ownership' has_one :current_ownership, -> { current: true, agreed: true }, class_name: 'ownership' has_many :next_ownerships, -> { 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, -> { 'return_date not null , borrowing_date not null', agreed: true }, class_name: 'loan' has_one :current_loan, -> { current: true, agreed: true }, class_name: 'loan' has_many :next_loans, -> { 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, -> { agreed: true, owning_date: nil, giving_date: nil }, class_name: 'ownership' has_many :possible_ownerships, -> { agreed: nil, owning_date: nil, giving_date: nil }, class_name: 'ownership' has_many :refused_ownerships, -> { 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, -> { agreed: true, borrowing_date: nil, return_date: nil }, class_name: 'loan' has_many :possible_loans, -> { agreed: nil, borrowing_date: nil, return_date: nil }, class_name: 'loan' has_many :refused_loans, -> { 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
in many places you're using has_many
if scope.
read on scopes here.
scopes allow define query can accessed method. example:
class loan < activerecord::base scope :current, where(current: true, agreed: true) end
can called so: product.first.loans.current
make sure put these scopes in appropriate models too, don't want filter loans in product model instance.
Comments
Post a Comment