location has_many :comments
comment belongs_to :location
出于某种原因,这个GET:
/locations/5/comments.json
行事像这样得到:
/comments.json
Started GET "/locations/5/comments.json" for 127.0.0.1 at 2012-04-10 21:18:00 -0700
Processing by CommentsController#index as JSON
Parameters: {"location_id"=>"5"}
Comment Load (0.1ms) SELECT "comments".* FROM "comments"
Completed 200 OK in 21ms (Views: 1.0ms | ActiveRecord: 0.7ms)
注意SQL查询:SELECT “意见” * FROM “意见”
路线设置是这样的:
resources :locations do
resources :comments
end
耙路线确认路线:
location_comments GET /locations/:location_id/comments(.:format) {:action=>"index", :controller=>"comments"}
这里是index动作:
def index
@comments = Comment.all
respond_to do |format|
format.json { render json: @comments }
end
end
这是正确的行动?这与结果是一致的,但我不知道还有什么应该在这里。我以前从来没有遇到嵌套资源的问题,所以我从来没有看过细节。
没有找到任何意外。这有什么问题? – prasvin 2012-04-11 04:57:42
生成的查询应该是SELECT“comments”。* FROM“comments”WHERE“comments”。“location_id”= 5. – 2012-04-11 12:00:41
不,嵌套不会使查询自动执行。如果是这样,你会如何得到所有的评论,而不考虑位置。可能是'@comments = Comment.all.without_nesting',它比传统方式更不清洁。 就你而言,你可以很容易地获得@Patrick建议的特定位置的评论。 – prasvin 2012-04-11 12:28:17