简体   繁体   English

路由和搜索数据库红宝石

[英]routing and searching a db ruby

I want to retrieve all the tweets that have a certain hashtag in them. 我想检索所有具有特定主题标签的推文。 At first I add the hashtags in my 2 tables : 首先,我在2个表中添加主题标签:

 def add_hashtags(tweet)
      tweet.content.scan(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/){ |tag|
        @allhashes = Hashtag.all
        @hash = Hashtag.find_by_name(tag[0].strip)
        unless @hash
          @hashtag = Hashtag.new(name: tag[0].strip)
          @hashtag.save
          @hashrel = Hashrelation.new(tweet_id: tweet.id, hashtag_id: @hashtag.id)
          @hashrel.save
        else
          @hashrel = Hashrelation.new(tweet_id: tweet.id, hashtag_id: @hash.id)
          @hashrel.save
        end
      }
    end

then I want to route to the show method of tweet controller : 然后我想路由到tweet控制器的show方法:

get 'tweets/show/(.:format)' => 'tweets#show', as: :hashtag

The links in the hashtags are as follows: 主题标签中的链接如下:

    def twitify(tweet = '')
      tweet.gsub(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/) do |tag|
        " " + link_to("#{tag.strip}", hashtag_path(tag.strip), {:name => tag.strip})
      end.html_safe
    end

And finally the show method of the tweet controller is : 最后,tweet控制器的show方法是:

  def show
    @hashtag = Hashtag.find_by_name(params[:name])
    @tweet_ids = Hashrelation.find_by_hashtag_id(@hashtag.id)
    @feed_items = Tweet.find_by_id(@tweets_ids.id)
  end

When I click on the link I get : 当我单击链接时,我得到:

undefined method `id' for nil:NilClass

which means that params[:name] is either nill or it isn't like the one I have in the DB. 这意味着params [:name]要么为零,要么不像我在数据库中的那个。

Could you guys help me figure this out ? 你们能帮我解决这个问题吗?

The link I see that is called is 'http://localhost:3000/tweets/show/.%23dadawea' which means I have extra things why would I ?. 我看到的链接称为'http://localhost:3000/tweets/show/.%23dadawea' ,这意味着我还有其他东西要干吗?

I would do the following 我会做以下

def add_hashtags(tweet)
  tweet.content.scan(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/).flatten.each do  |tag|
    hashtag = Hashtag.where(name: tag.strip).first_or_create
    Hashrelation.create(tweet_id: tweet.id, hashtag_id: hashtag.id)
  end
end

Then change the twitify method to look like 然后将twitify方法更改为

def twitify(tweet = '')
  tweet.gsub(/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/) do |tag|
    " " + link_to("#{tag.strip}", hashtag_path(name: tag.strip))
  end.html_safe
end

And the show method 和显示方法

def show
  @hashtag = Hashtag.find_by_name(params[:name])
  @tweet_ids = Hashrelation.where(hashtag_id: @hashtag.id).pluck(:id)
  @feed_items = Tweet.where(tweet_id: @tweets_ids)
end

This should be what you are looking for. 这应该是您想要的。 Now for whats changing: 现在,发生什么变化:

  • Removed Duplicate logic in the add_hashtags to use create instead. 删除了add_hashtags中的重复逻辑以改用create。

  • twitify method is passing name as an html option not a url option so I fixed that. twitify方法将名称作为html选项而不是url选项传递,因此我将其修复。 Right now it thinks you want to set the format to the name of the hashtag and name the link the name of the hashtag** 现在,它认为您想将格式设置为主题标签的名称,并将链接命名为主题标签的名称**

  • show method is using find_by which will only return a single result not what you wnat for tweet_ids so i changed it to where clause and just grabbed the ids. show方法使用的是find_by,它只会返回一个结果,而不是您为tweet_ids拥有的结果,因此我将其更改为where子句,只是获取了id。 Then changes feed_items to search Tweet for all tweet_id s in the Array. 然后更改feed_items以在Tweet中搜索数组中的所有tweet_id

To strip the # just use tag.strip[1..-1] 剥离#只需使用tag.strip[1..-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM