开发者

In rails, how do I generate a model path (with a custom to_param), without loading the model?

I have a situation where I need to generate links for a group of models without loading the models in question. This is complicated by the fact that I have overridden to_param in my model to produce a slug as the identifier which is constructed by the concatenation of several fields.

For normal operations, this isn't a big deal as the object is loaded. However in this particular scenario - rendering links from search results - I have the necessary fiel开发者_如何学编程d values, (they are all indexed), but I have no model objects. I would like to avoid having to retrieve each of the models just to render a URL.

Here is a pseudo-model :

class MyModel
  include Mongoid::Document

  field :name
  field :location
  .
  <[extraneous bits removed]>
  . 
  def to_param
    [ name, location ].map(&:parameterize).join('-')
  end
end

Is there a way to "fake" the model creation so I can pass it in to the normal <model>_path url generation methods?

What alternatives are there for this situation?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜