What is the proper way to form this multi-dimensional array? - ruby-on-rails

I am attempting to perform a series of requests that will organize the results in a given date span in a way where each location will live on the top level, and contain an another array with each set of results. Here is what I have so far:
locations = Location.all
#requests = []
locations.each do |location|
request = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
#requests.push(location => request)
end
My ideal (non-working) implementation in the view would look something like:
<ul>
<% #requests.each do |location| %>
<li>location[0].name</li>
<ul>
<% location[1].each do |request| %>
<li>request.name</li>
<% end %>
</ul>
<% end %>
</ul>

Try something like this:
#requests = {}
Location.all.each do |location|
#requests[location.name] = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
end
and:
<ul>
<% #requests.each do |location_name, request_list| %>
<li><%= location_name %></li>
<ul>
<% request_list.each do |request| %>
<li><%= request.name %></li>
<% end %>
</ul>
<% end %>
</ul>
#requests becomes a hash with the key being the location name and the value being the purchase requests.

Related

How to Make a TODO list item when a database cell matches today's date

I'd like to create a todo list looking for any cell that matches today's date on either agreement, start or due date.
<% #project.where("project_date_agreement + project_date_start + project_date_due > ?", Date.today).each do |tasks_today| %>
<ul>
<li>Item Due Today</li>
</ul>
<% end %>
Any help getting me in right direction would be appreciated.
You want to use OR conditions in your where clause. Properly speaking this would be in your controller.
#projects = Project.where('date_agreement = ? OR date_start = ? or date_due = ?', Date.today, Date.today, Date.today)
In your Project Model you might want to create a field that say's what's due...
def due_because
due_array = []
due_array << 'Agreement date' if date_agreemnt == Date.today
due_array << 'Start date' if date_start == Date.today
due_array << 'Due date' if date_due == Date.today'
due_array.join(', ')
end
Then in your view you would iterate over the #projects
<ul>
<li>Items Due Today</li>
<% #projects.each do |project| %>
<li><%=project.name%> <%=project.due_because%></li>
<% end %>
</ul>
if you asking one row and different column, how about if you combine with table
<% #tasks = #projects.where("date_agreement = ? AND date_start = ? AND date_due = ?",Date.today ,Date.today, Date.today) %>
<table>
<tr>
<ul>
<% #tasks.each do |task| %>
<td>
<li><%= task.check_box :item_due %></li>
</td>
<% end %>
</ul>
</tr>
</table>

How can I get the model name of object in rails?

I got four different models.
Here's an example,
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
then how to check which model #production is?
I tried
<% #production.each do |p| %>
<%=p.class.name%>
<% end %>
but it returns "Array"
It seems to be simple, but I can't find out
(I edited question)
The problem is here
#single = Single.all
#coe = Coe.all
#blend = Blend.all
#production = #single+#coe+#blend
change these lines with
#single = Single.all.to_a
#coe = Coe.all.to_a
#blend = Blend.all.to_a
#production = #single+#coe+#blend
and then if you will check
#production.first.class.name #Single
#production.last.class.name #Blend
so in your view you can do this
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
if while iterating on #production it returns array so you need to try this.
<% #production.each do |p| %>
<% p.each do |product| %>
<%= product.class.name %>
<% end %>
<% end %>
#production is a collections of combinations of single, coe, and blend thats why #production.class.name doesnt work, you need to iterate each object like this:
<% #production.each do |object| %>
<%= object.class.name %>
<% end %>

rails showing blank page

In my rails app I need to implement a conditional search form .There are two tables named tweets and coordinates , but the output is showing a blank page and I am stuck up with dis.
my coordinates_controller
class CoordinatesController < ApplicationController
def home
end
# def paramas(b)
#
# #b = params[:show]
# return #b
end
#def coor(latitude,longitude)
# #latitude=0
##longitude=0
#end
def query
#a=Coordinates.find("city=?", params[:show])
if(params[:show]= a.city) then
latitude= a.latitude
longitude=a.longitude
end
if(latitude=0 && longitude=0) then
return sql="Select * from tweets where tweet_text LIKE '%text%' AND user_loc LIKE 'params[: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 'params[:show]') ) order by id desc"
else
return sql="Select * from tweets where tweet_text LIKE '%text%'"
end
end
end
My Tweets_controller
class TweetsController < ApplicationController
include CoordinatesHelper
def search
render 'tweets/search'
end
def index
# include CoordinatesHelper
sql=query
#tweets=Tweets.paginate_by_sql(sql, :page => #page, :per_page => #per_page )
#render 'tweets/index'
end
end
my view code for the search button
<%=form_tag({controller: 'tweets', action:'index'}, method: "get") do %>
<%=label_tag(:search, "Search for:") %>
<%=text_field_tag(:text) %>
<%=label_tag(:show, "Show for:") %>
<%=text_field_tag(:show) %>
<%= submit_tag( "GO" ) %>
<% end %>
my view code to display the results
<%= will_paginate #tweets %>
<% #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 %>
But for some reasons the output is showing an empty page tried editing several tymes but in vain. Anybody kindly help me wid dis, I am struggling wid dis from a long tym
In your query method, you wrote and finder method, but you have write table name there, not model name, finders works on Model name, write like this :
In your query method replace this :
#a=Coordinates.find("city=?", params[:show])
with this :
#a=Coordinate.find("city=?", params[:show])
because model name, always remain singular, unless you make changes any configuration.
and I think, there is a problem with your params[:show] with this. Please paste your result of 'params[:show]'
Hope it will help. Thanks

rails 3 'undefined method' when a method is called inside another method

I have two tables called coordinates and tweets in my database, and I need to perform a search to fetch the tweets from the tweets table. Since this also requires data from coordinates table, I decided to use require and then create an object and then call the function of the other program inside the other program.
My coordinates_controller:
class CoordinatesController<ApplicationController
def paramas(b)
#b = params[:show]
return #b
end
def coor(latitude,longitude)
#latitude=0
#longitude=0
end
def search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#a = Coordinates.where(city :params[:show] ).take
if(params[:show]== a.city) then
#latitude= a.latitude
#longitude=a.longitude
end
if(#latitude=0 && #longitude=0) then
return #sql="Select * from tweets where tweet_text LIKE '%search%' AND user_loc LIKE 'a.paramas' 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 '%search%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'a.paramas') ) order by id desc"
else
return #sql="Select * from tweets where tweet_text LIKE '%search%'"
end
end
end
# a= CoordinatesController.new
end
My tweets_controller
require 'coordinates_controller.rb'
#require 'models/coordinates.rb'
class TweetsController<ApplicationController
def show
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
my view code for the search button
<%= form_tag({controller: "tweets", action:"show" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= submit_tag("Search") %>
<% end %>
<%= form_tag({controller: "coordinates", action:"search" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
My view code for displaying results
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></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>
<% end %>
</ul>
I am getting the error undefined method min_latitude. I can't figure out what is causing the error.
When you call your method search Ruby don't know what min_latitude means here
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)

undefined method `parameters' for nil:NilClass in rails 3

in my rails app i have two controllers coordinates and tweets and their tables respectively.i have two text fields in my search button and i need to fetch queries from tweets table using the conditions in coordinates table . But I am getting this error "undefined method `parameters' for nil:NilClass" .
My coordinates_controller.rb
class CoordinatesController<ApplicationController
def paramas(b)
#b = params[:show]
return #b
end
def coor(latitude,longitude)
#latitude=0`enter code here`
#longitude=0
end
def search
#a=Coordinates.where(city: params[:show]).take
if(params[:show]== a.city) then
#latitude= a.latitude
#longitude=a.longitude
end
if(#latitude=0 && #longitude=0) then
return #sql="Select * from tweets where tweet_text LIKE '%search%' AND user_loc LIKE 'a.paramas' 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 '%search%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'a.paramas') ) order by id desc"
else
return #sql="Select * from tweets where tweet_text LIKE '%search%'"
end
end
end
# a= CoordinatesController.new
end
class TweetsController<ApplicationController
require 'coordinates_controller.rb'
#require 'models/coordinates.rb'
def index
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
My view code for the search button
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:search) %>
<%= text_field_tag(:show) %>
<%= submit_tag("get results ") %>
<% end %>
My view code to display tweets
%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></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>
<% end %>
</ul>
Iam stuck up with this for a long time anybody pls help me to proceed. thks in advance
Suggestion:
Install dev gem better_errors: https://github.com/charliesome/better_errors
With it you will get a nice console in your browser when your code fails.
So if I where you, I would insert some bogus code:
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
likethisboguscodeBLABLABLA
end
end
To make sure it fails after this line, then in the console, check that #tweets etc is actually objects and so on. Repeat on every crucial line of code that could cause this problem.

Resources