开发者

Rails 3 design for a has_many :through

开发者 https://www.devze.com 2023-01-30 15:34 出处:网络
I have a Model that represents a purchase class Purchase < ActiveRecord::Base belongs_to :user belongs_to :item

I have a Model that represents a purchase


class Purchase < ActiveRecord::Base
  belongs_to :user
  belongs_to :item

So basically each purchase belongs to both a user and an item that is being purchased. Now here is my problem, I could nest purchase on item, so that when user is purchasing he will go to:

/item/1/purchase/new

That would work fine, but I also like the user to be able to go to /user/1/purchases/1/edit etc.Basically purchase would need to be nested onto both user and item which you can't.

Right now I am 开发者_运维问答basically adding more restful actions to both the user and item, like /item/1/purchase and /user/1/purchase/ etc, but that leads to growing the controllers quite a lot. Is this the only way or could you nest a resource to 2 resources ?


Sure, they can nest in both. Rails won't stop you from making whatever routes you want.

However, I'd recommend nesting only certain actions under certain parent resources. For instance, new and create can go under items, since that makes sense as you're reaching the purchase from the item, but show and the like might make more sense under the user, if it really needs to be nested under anything at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消