使用stock_quote gem的股票报价作为acts_as_taggable gem的标签?

Joe*_*ith 6 ruby ruby-on-rails

所以我使用的是acts_as_taggable gem提供的标签.帖子是我标记的内容.我怎么能说一些(pseduocode)=>

if a collection of Posts has a tag with a corresponding StockQuote, display the stock quote
Run Code Online (Sandbox Code Playgroud)

所以现在我有一个使用acts_as_taggable的Post资源.这是我的帖子索引操作现在的样子:

def index
@stock = StockQuote::Stock.quote("symbol")

if params[:tag]
@posts = Post.tagged_with(params[:tag])
else
 @posts = Post.order('cached_votes_score desc')
end

end
Run Code Online (Sandbox Code Playgroud)

所以我会以某种方式迭代所有@posts tagged_with params [:tag]并将标签与股票报价进行比较.如果匹配,则在所有@posts tagged_with params [:tag]的索引上显示引号.我将试着弄清楚如何限制每个帖子很快就有1个标签

如果该标签恰好是股票报价,我如何使用帖子的:标签来访问股票报价?

jer*_*ark 0

我想你会想做类似的事情:

@symbols = @post.tag_list
@quotes = StockQuote::Stock.quote(@symbols)
Run Code Online (Sandbox Code Playgroud)

由于帖子实际上可以用多个符号标记,因此您需要一次获取所有符号,然后迭代视图中的结果。就像是:

<% @quotes.each do |quote| %>
  <%= "#{quote.symbol}: #{quote.ask}" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

我对宝石不太熟悉,但快速浏览一下,似乎这样的东西会起作用。

另外,您可以将问题表述为好像您有一个帖子的单个实例,但随后显示您的索引操作,该操作将拉取帖子的集合。我提供的示例适用于单个帖子(查看帖子页面的用户)。