simha
simha

Reputation: 574

Should a model that has a belongs_to relationship with 2 other models be nested under them in routes?

I am creating a web app for managing personal real estate . So I have the following models

class Building < ActiveRecord::Base
  attr_accessible :address, :name
  has_many :docs
end

class Land < ActiveRecord::Base
  attr_accessible :address, :name
  has_many :docs
end

class Doc < ActiveRecord::Base
  #Model for documents
  attr_accessible :name ,:actual_file
  has_attached_file :pdf
  belongs_to :building
  belongs_to :land
end

Now, What would be the best way to route this ? Should I nest docs separately in both building and land resource ? Or Is it better to not nest docs at all ? I know I could use polymorphic associations, but assume that I don't want to use them . Thsi question is more about the routing part.

This is my routes.rb

  resources :lands
  resources :buildings       
  resources :docs

What are the advantages of each approach ?

Upvotes: 1

Views: 55

Answers (2)

Thaha kp
Thaha kp

Reputation: 3709

Try this approach...

  resources :lands do
    resources :docs
  end

  resources :buildings do
    resources :docs
  end

Then you can access docs like

/lands/:id/docs/:id
/buildings/:id/docs/:id

more readable also...

See more about nested routing here

Upvotes: 1

zwippie
zwippie

Reputation: 15515

This sounds like a good case for Routing Concerns.

Although I am not sure this works in Rails 3 and without polymorphic relations.

Upvotes: 2

Related Questions