簡體   English   中英

從Rails 3中的另一個控制器調用模型中的方法

[英]calling a method in a model from another controller in rails 3

在我的Rails應用程序中,我需要從另一個控制器調用一個表中定義的方法。 有兩個表,分別稱為坐標和推文。 推文表的條件由坐標表決定。 在我的最終視圖中,我需要顯示推文表的屬性,條件由坐標表決定。 我的座標表代碼

  class Coordinates<ActiveRecord::Base
      def self.query()
        a = Coordinates.where("city=?", params[:show])
        b = a.first
        if a.count == 1
          latitude = b.latitude
          longitude= b.longitude
          if(latitude=0 && longitude=0) then
            return  sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE '%show%' order by id desc"
          else if (latitude!=0 && longitude!=0) 
                 min_lat = latitude - 1.0
                 max_lat = latitude + 1.0
                 min_lng = longitude - 1.0
                 max_lng = longitude + 1.0
                 return   sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE '%show%') ) order by id desc"
               else
                 return   sql="Select * from  tweets where tweet_text LIKE  '%text%'"
               end    
          end
        end

我的推文表

class Tweets<ActiveRecord::Base
  attr_accessible  :id, :tweet_created_at, :tweet_id, :tweet_text, :tweet_source, :user_id, :user_name, :user_sc_name, :user_loc, :user_img, :longitude, :latitude, :place, :country

end

我需要從我的tweets_controller調用查詢定義,以便它決定從tweets表中獲取什么查詢並顯示在最終視圖中。 但是params函數在model.rb文件中不起作用。 我想要這樣的東西My tweets_controller.rb

class TweetsController<ApplicationController
  def index
    Coordinates.query()
  end
end

我的最終查看代碼

<%= @tweets.each do |tweets| %>

<ul>

  <li><%= tweets.id %></li>
  <li><%= tweets.tweet_created_at %></li>

  <li><%= tweets.tweet_source %></li>
  <li><%= tweets.tweet_text %></li>
  <li><%= tweets.user_id %></li>
  <li><%= tweets.user_name %></li>
  <li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
  <li><%= tweets.user_img %></li>
  <li><%= tweets.longitude %></li>
  <li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
  <li><%= tweets.country %></li>

</ul>
<% end %>

我無法從tweetscontroller調用在坐標表中定義的查詢功能。 我也嘗試使用輔助方法,但這似乎無濟於事,聽起來很復雜。 有人請幫助我

實際上,我發現您的代碼存在一些問題。

首先,請使用有意義的名稱。 您正在檢索與某個城市有關的推文。 其次,在您的query方法中,您要嘗試做的大部分事情是檢索推文,該推文應該在推文模型中。

您的代碼做錯了什么:

  • 它只是建立SQL?
  • 它搜索包含固定文本%text%%text% ,我假設這應該是給定的搜索詞
  • 它搜索給定的用戶位置%show% ,我假設它應該是城市名稱(您的params[:show]

我的建議是:

  • 發現鳴叫應該是在Tweet模型
  • 使用更小的方法
  • 為簡單起見,我假設您的搜索詞是params[:text]

因此,我將您的代碼編寫如下:

class TweetsController < ApplicationController

  def index
    city = params[:show]
    search_term = params[:text]
    city_coordinates = Coordinates.where('city=?', city)
    @tweets = if city_coordinates.count == 1 && city_coordinates.first.valid_location?
                Tweet.for_coordinate(city_coordinates.first)
              else
                Tweet.for_user_location(city)
              end
    @tweets = @tweets.where("tweet_text like ?", "%#{search_term}%")
  end
end

不要自己構建sql,讓activerecord為您完成。 另外: where很懶,所以您可以輕松地將它們鏈接起來。

然后在您的Coordinate模型中添加

class Coordinate < ActiveRecord::Base

  def valid_location?
    self.latitude != 0 && self.longitude != 0
  end

end

並在您的Tweet模型中

class Tweet  < ActiveRecord::Base

  def self.for_coordinate(coordinate)
    bbox = { min_lat: coordinate.latitude - 1.0, max_lat: coordinate.latitude + 1.0,
             min_lng: coordinate.latitude - 1.0, max_lng: coordinate.latitude + 1.0
           }
    Tweet.where("(longitude BETWEEN ? and ?) AND (latitude BETWEEN ? and ?)) OR (user_loc LIKE ?)", 
       bbox[:min_lng], bbox[:max_lng], bbox[:min_lat], bbox[:max_lat], "%#{coordinate.city}%")

  end

  def self.for_user_location(city)
    Tweet.where("user_loc like ?", "%#{city}%")  
  end

end        

在模型的self.query方法中,設置如下參數:

class Coordinates<ActiveRecord::Base

      def self.query(something)
        a = Coordinates.where("city=?", something)
        b = a.first
        if a.count == 1
          latitude = b.latitude
          longitude= b.longitude
          if(latitude=0 && longitude=0) then
            return  sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE '%show%' order by id desc"
          else if (latitude!=0 && longitude!=0) 
                 min_lat = latitude - 1.0
                 max_lat = latitude + 1.0
                 min_lng = longitude - 1.0
                 max_lng = longitude + 1.0
                 return   sql = "Select * from tweets where tweet_text LIKE '%text%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE '%show%') ) order by id desc"
               else
                 return   sql="Select * from  tweets where tweet_text LIKE  '%text%'"
               end    
          end
        end

在您的控制器中,將params[:show]作為參數傳遞:

class TweetsController<ApplicationController
 def index
  @tweets = Coordinates.query(params[:show])
 end
end

現在應該可以了。 謝謝

您調用Coordinates.query()但實際上並未存儲返回值。

嘗試這個:

@tweets = Coordinates.query()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM