routing - Rails 4.2+: deep nesting resources when it's required to display data from entire resource chain -
i'm familiar guideline not nest more 1 level deep, , understand various discussions having shortest urls possible both in , out of code.
most of stackoverflow questions , googling i've done answer presenting use cases 1 doesn't have access elements in entire chain.
but do when have access parent resources further chain every page you're working with?
/{account_slug}/applications/{application_uuid}/borrower/employments/{uuid}
when dealing single employment
record, have display account-specific information take parent account
object via account_slug
, application
information. , technically, borrower
db query, since there 1 each application, don't have add id/slug in url.
general advice like:
/employments/{uuid}
but if that, in controller code (and elsewhere) still have do:
@employment = employment.find_by(uuid: params[:uuid]) @account = @employment.borrower.application.account
so have walk entire parent association chain , execute association db queries. how 1 resolve kind of situation deep nested association has kept, first parent last child?
1: add association attributes children
class employment belongs_to :borrower belongs_to :application belongs_to :account end
now i've got bunch of associations going on everywhere, walk clear chain. seems "dependency hell" me?...
2: go deep nested routes; github it
i've noticed github employs deep nested routes:
github.com/{username}/{repo}/settings/hooks
while not deeply-nested use case, still nest underneath username
, repo
, , if list out verbose url is:
/github.com/accounts/{username}/repos/{repo_name}/settings/hooks
is aware if github has method of optimizing deep nesting, or looking account
, repo
every request , biting db query overhead (probably not big deal...)?
3: deep nest routes, make own url helpers keep clean
using /{acount_slug}/applications/{application_uuid}/borrower/employments/{uuid}
, url helper like:
account_applications_borrower_employments_path(@account, @application, @borrower, @employment)
this cleaned using helpers:
def borrower_employment_path(@employment) @borrower = @employment.borrower @application = @borrower.application @account = @application.account account_applications_borrower_employments_path(@account, @application, @borrower, @employment) end
very detailed question. kinda late respond, in past when problem of nested resources has bitten me, ends need revisit way relationally modeled database.
of possible solutions suggested, number 1 best considering opinionated nature of rails framework. rails associations powerful , leveraging them useful app scales.
addressing concerns dependencies between models:
without seeing schema, can guess, modeling this:
class account has_many :applications end class application belongs_to :account has_one :borrower has_one :employment end class borrower belongs_to :application end class employment belongs_to :application has_one :account, through: :application end
this best guess dissecting info above. modeling, able in controller action:
def action_name @employment = employment.find_by(uuid: params[:uuid]) @account = @employment.account end
you able add associations, say, if wanted do: @employment.borrower
by adding:
class employment belongs_to :application has_one :account, through: :application has_one :borrower, through: :application end
i give read every once , while keep fresh:
http://guides.rubyonrails.org/association_basics.html#the-types-of-associations
rails has discouraged heavily nested routing, despite github's implementation of application:
Comments
Post a Comment