Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling a method in a model from another controller in rails 3
    primarykey
    data
    text
    <p>In my rails application I need to call a method defined in one table from another controller. There are two tables named coordinates and tweets. The condition for the tweets table is decided by the coordinates table. In my final view I need to display the attributes of the tweets table for which the condition is decided by the coordinates table. My Coordinates table code </p> <pre><code> class Coordinates&lt;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 &amp;&amp; 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 &amp;&amp; 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 </code></pre> <p>My tweets table </p> <pre><code>class Tweets&lt;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 </code></pre> <p>I need to call the query definition from my tweets_controller so that it decides what query to fetch from the tweets table and display in the final view . But the params function is not working in the model.rb file . I want something like this My tweets_controller.rb</p> <pre><code>class TweetsController&lt;ApplicationController def index Coordinates.query() end end </code></pre> <p>My final view code</p> <pre><code>&lt;%= @tweets.each do |tweets| %&gt; &lt;ul&gt; &lt;li&gt;&lt;%= tweets.id %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.tweet_created_at %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.tweet_source %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.tweet_text %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.user_id %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.user_name %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.user_sc_name %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.user_loc %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.user_img %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.longitude %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.latitude %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.place %&gt;&lt;/li&gt; &lt;li&gt;&lt;%= tweets.country %&gt;&lt;/li&gt; &lt;/ul&gt; &lt;% end %&gt; </code></pre> <p>I am not able to call the query function defined in coordinates table from tweetscontroller. Also I tried using helper methods but that does not seem to help and sounds very complicated. Anybody kindly help me with this</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload