Group params in rails 3.2 - ruby-on-rails

I have a search form with two drop down options with varieties (params[:variety_one], params[:variety_two]). Once submitted I want to display the results of both varieties if they only exist in the trial_id column. The variety_id column has multiple enteries per trial_id.
e.g
trial_id | variety_id
444 | 2300
444 | 2255
444 | 3450
445 | 2300
445 | 3450
446 | 2300
446 | 2255
446 | 5567
So if the user selected varieties 2300 and 2255 then it would only hit trial_id's 444 and 446 not 455 as variety 2300 only exists.
This is my controller so far.
def index
all = Result.select(:variety_id)
#variety = Variety.where(:variety_id => all).order('variety_name DESC')
#years = Result.select('DISTINCT year')
#regions = Region.all
#irrigations = Trial.select('DISTINCT irrigated').order('irrigated ASC')
end
def search
#comparison = Result.group(:trial_id).where(variety_id: [params[:variety_one], params[:variety_two]]).having('COUNT(*) = 2').joins(:trial).where('trials.irrigated' => params[:irrigated], 'year' => params[:year]).joins(:regions).where('sites.region_id' => params[:regions])
end
This is the error I am getting
Mysql2::Error: Column 'trial_id' in field list is ambiguous: SELECT COUNT(DISTINCT results.trial_id) AS count_distinct_results_trial_id, trial_id AS trial_id FROM `results` INNER JOIN `trials` ON `trials`.`trial_id` = `results`.`trial_id` INNER JOIN `trials` `trials_results_join` ON `trials_results_join`.`trial_id` = `results`.`trial_id` INNER JOIN `sites` ON `sites`.`site_id` = `trials_results_join`.`site_id` INNER JOIN `regions` ON `regions`.`region_id` = `sites`.`region_id` WHERE `results`.`variety_id` IN (2300, 2255) AND `trials`.`irrigated` IN (0, 1, 2) AND `results`.`year` IN (2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014) AND `sites`.`region_id` IN (1, 2, 3, 4, 5, 6, 7, 8) GROUP BY trial_id HAVING COUNT(*) = 2
If i remove the group(:trial_id) it works, but it grabs all the variety_id's selected.
Form
<% #options = ['Dryland', 'Irrigated', 'Semi-irrigated'] %>
<%= form_tag vpc_search_path do %>
<div class="row-fluid">
<div class="span2"></div>
<div class="span4" style="text-align: center; padding: 10px 0 10px 0;">
<h5 style="padding-bottom: 10px; text-align: center;">Select Variety One</h5>
<%= select_tag :variety_one, options_from_collection_for_select(#variety, "variety_id", "variety_name", :selected => "2300"), include_blank: false %>
</div>
<div class="span4" style="text-align: center; padding: 10px 0 10px 0;">
<h5 style="padding-bottom: 10px; text-align: center;">Select Variety Two</h5>
<%= select_tag :variety_two, options_from_collection_for_select(#variety, "variety_id", "variety_name", :selected => "2255"), include_blank: false %>
</div>
<div class="span2"></div>
</div>
<div class="row-fluid">
<div class="span12" style="text-align: center; padding: 10px 0 10px 0;">
<h5 style="padding-bottom: 10px; text-align: center;">Trial Type</h5>
<% #irrigations.each do |i| %>
<div class="checkbox inline"><%= check_box_tag 'irrigated[]', i.irrigated, true %>
<% if i.irrigated == 0 %> <%= #options[0] %> <% end %>
<% if i.irrigated == 1 %> <%= #options[1] %> <% end %>
<% if i.irrigated == 2 %> <%= #options[2] %> <% end %>
</div>
<% end %>
</div>
</div>
<div class="row-fluid">
<div class="span2"></div>
<div class="span4" style="text-align: center; padding: 10px 0 10px 0;">
<h5 style="padding-bottom: 10px; text-align: center;">Select Years</h5>
<% #years.each do |y| %>
<%= check_box_tag 'year[]', y.year, true %> <%= y.year %></br>
<% end %>
</div>
<div class="span4" style="padding: 10px 0 10px 85px;">
<h5 style="padding-bottom: 10px;">Select Regions</h5>
<% #regions.each do |r| %>
<%= check_box_tag 'regions[]', r.region_id, true %> <%= r.name %></br>
<% end %>
</div>
<div class="span2"></div>
</div>
<div class="row-fluid">
<div class="span12" style="text-align: center; padding-top: 10px;">
<%= submit_tag "Compare" %>
<% end %>
</div>
</div>

def index
results_ids = Result.pluck('variety_id') ## use pluck here
#variety = Variety.where(:variety_id => results_ids).order('variety_name DESC')
#years = Result.select('DISTINCT year') # do you really need an object here? pluck?
#regions = Region.all # ok, all, really? large table?
#irrigations = Trial.select('DISTINCT irrigated').order('irrigated ASC')
end
def search
#comparison = Result.group('results.trial_id')
.where(variety_id: [params[:variety_one], params[:variety_two]])
.having('COUNT(*) = 2').joins(:trial)
.where('trials.irrigated' => params[:irrigated], 'year' => params[:year])
.joins(:regions).where('sites.region_id' => params[:regions])
end
generally speaking, whenever you get column is ambiguous you got to attach table name with it in your request cause rails is not able to form the full request to database itself

Related

Index page is not re rendering/refreshing on search in Rails

I am making a simple search in rails on the index page. My problem is that on debug I get the correct results but they don't show on the frontend.
def index
if !params[:category].present? and !params[:title].present?
#services = Service.take(4)
else
if params[:category] != "Find Service" and params[:title].present?
#services = Service.where(category: params[:category], title: params[:title]).take(4)
elsif params[:category] == "Find Service" and params[:title].present?
#services = Service.where(title: params[:title]).take(4)
elsif params[:category] != "Find Service" and !params[:title].present?
#services = Service.where(category: params[:category]).take(4)
else
#services = Service.take(4)
end
end
end
My view index.html.erb
<%= form_with(url: "services", method: "get") do |form| %>
<div class="banner-input" style="margin-top">
<div class="listing-sort listing-sort--main-listing" style="width: 50%;float: left; border-radius: 6px; border: 2px solid white; background: transparent; padding: 0px;">
<input type="submit" value="" class="main-listing__form-field agent-submit" style="width: 10% ;float:right">
<%= form.text_field "title", id: "search_agent", class: "main-listing__form-field ", placeholder: "Search by Name or Location...", style: "width: 75% ;float:right; background: transparent; color: white!;" %>
<div class="listing-sort__inner" style="float:left;margin: 0px; width: 25%; padding: 0px;">
<p class="listing-sort__sort">
<%= form.select("category",options_for_select(["Find Service", "Assembly", "Carpet Cleaning", "Electrical", "House Cleaning", "HVAC", "Handyman", "IT", "Junk Removal", "Lawn Care","Moving", "Painting", "Plumbing", "Staging", "Photography", "Videography", "Inspectors"], :selected => "Find Service"), {include_blank: false}, { :class => 'ht-field listing-sort__field' }) %>
</p><!-- .listing-sort__sort -->
</div><!-- .listing-sort__inner -->
</div><!-- .listing-sort -->
</div>
<% end %>
<section style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Recently viewed & more</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
<section class="jumbotron"style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Pro SEM services
</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
This is _service_card
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3" style="padding-bottom: 10px">
<div class="boxes">
<div class="box-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail-main">
<div class="user-detail-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail">
<a><h3><%= f.user.profile.first.first_name %></h3></a>
<p>Level 1 Seller</p>
</div>
<div class="user-detail-para">
<% byebug %>
<p><%= f.description %></p>
</div>
<div style="padding-top: 50px;">
<i class="fas fa-star" style="color: #ffbf00"></i> (5)
</div>
</div>
<div class="user-detail-footer">
<div class="heart-icon">
<i class="fas fa-heart" style="color: #c6c6c6"></i>
<i class="fas fa-bars" style="color: #c6c6c6"></i>
</div>
<div class="user-value">
<p>Starting At $ <%= f.packages.first.price%></p>
</div>
</div>
</div>
Please help me regarding this. Looking forward. It is working fine without search, but with search it doesn't update the results and show same results.

How to hide radio_button and use a div/label as the button instead? (Ruby on Rails)

In a simple_form I'm trying to style the radio_buttons.
But how do I hide the radio_buttons and select a table_id by clicking on the label?
<% Table.all.each do |rt| %>
<div class="btn-group" data-toggle="buttons-radio">
<%= f.radio_button :table_id, "fd", :id=>"hi-#{rt.id}", :style=>"display:none;" %>
<label for="hi-#{rt.id}" class="btn btn-primary box3"><%= rt.id %>
</label>
</div>
<% end %>
Notes:
table has_many :reservations
reservation belongs_to :table
table_id is a integer-column in reservations table.
Screenshot of the buttons
I found an answer.
View:
<% Table.all.each do |rt| %>
<label class="btn btn-primary box3" for="reservation_table_id_<%=rt.id%>">
<%= f.radio_button :table_id, rt.id %>
<span><%= rt.id %></span>
</label>
<% end %>
Style:
label > input{
visibility: hidden;
position: absolute;
}
label > input + span{
cursor:pointer;
border:2px solid transparent;
}

Rails - Group_by method doesn't work

i'm trying to group items (games) by month in rails and SQL3 but it generates this :
Error
March 17th
<% #game_months.each do |month, games| %>
<% for game in games %>
<a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
<div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">
Here is my games controller
class GamesController < ApplicationController
def index
#game = current_user.games
#games = Game.all.order("created_at DESC")
#game_months = #games.group_by {|g| g.created_at.beginning_of_month }
end
My view
<% #game_months.each do |month, games| %>
<% for game in games %>
<a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
<div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">
<div class="item b-b">
<div><%= image_tag game.photos[0].image.url(:medium), class:"img-responsive" if game.photos.length > 0 %></div>
</div>
<div class="p-a text-left white">
<h6 class="text-md text-black block p-b-sm"><strong><%= game.game_name %></strong></h6>
<span class="text-muted p-t-md"><%= game.game_description %></span>
<div class="m-t-md">
<span><%= image_tag avatar_url(game.user), class:"w-24 circle b-a" %></span>
<span class="m-t-sm text-black pull-right text-md"></span>
</div>
</div>
</div>
</a>
<% end %>
<% end %>
I don't know what i'm doing bad. Please help, i don't know what to do to solve this!
CHEERS! :)
Change
#games = Game.all.order("created_at DESC")
#game_months = #games.group_by {|g| g.created_at.beginning_of_month }
To
#game_months = Game.all.group_by { |g| g.created_at.month }
It should produce hash looking like this:
{1=>[GameObject1], 2=>[GameObject3, GameObject4], 3=>[GameObject2] ...}
Now iterate over hash and do what you want to do.

How to sort keys/values in Ruby/Rails by file extension?

I have a Hash that contains filenames. Each of these filenames have different types of extensions.. i.e. (.txt, .bam, .bam.bai, .fastq, etc) I am trying to figure out a way of iterating over these keys and sorting the list into (3) Three sections based on filename in HTML/erb.... Like this.
Section 1
All FastQ files (.fastq)
Section 2
All .bam and bam.bai files (.bam, .bam.bai)
Section 3
All text files (.txt)
Basically what I have so far is this...
<div class="content_box_title">Sequencing Results</div>
<div class="content_box">
<% seqfiles = #vmsr_project.seqfiles %>
<% seqfiles.keys.sort.each do |file|
fpath = file.to_s
fsize = File.size(file)
fdate = File.mtime(file)
`enter code here`fsize = fsize / 1000000
fsize = fsize.to_int
fsize = fsize.to_s
if fsize == "0" then fsize = "EMPTY or <1" end
%>
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => #vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
<div style="float: left">
<!-- <%= fpath %> -->
</div>
<% end %>
<% if seqfiles.empty? %>
<ul><li>None</li></ul>
<% end %>
</div>
<div class="content_box_title">Aligned</div>
<div class="content_box">
<p>.bam, .bam.bai files display here</p>
</div>
<div class="content_box_title">QC</div>
<div class="content_box">
<p>.txt files display here</p>
</div>
As an update everyone... I got the following to work (Please don't laugh, I am a newbie) but the issue is now of course, the header:
Sequencing Results
prints each time it parses in all three sections. Definitely need a refactor.
<% seqfiles.keys.sort.each do |file|
fpath = file.to_s
fsize = File.size(file)
fdate = File.mtime(file)
fsize = fsize / 1000000
fsize = fsize.to_int
fsize = fsize.to_s
if fsize == "0" then fsize = "EMPTY or <1" end
case fpath
when /\.fastq.gz$/ %>
<div class="content_box_title">Sequencing Results</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => #vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% when /\.bam(\.bai)?$/ %>
<div class="content_box_title">Aligned</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => #vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% else %>
<div class="content_box_title">QC</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => #vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% end %>
<% end %>
<% if seqfiles.empty? %>
<ul><li>None</li></ul>
<% end %>
Just separate filenames into 3 arrays and then iterate each.
<% separated = seqfiles.keys.sort.inject({}){|h,key|
case key.to_s
when /\.fastq$/
h[:fastq] ||= []
when /\.bam(\.bai)?$/
h[:bam] ||= []
else
h[:other] ||= []
end.push(key)
h
} %>
<% separated[:fastq].each do |file| %>
...
<% end %>
<% separated[:bam].each do |file| %>
...
<% end %>
<% separated[:other].each do |file| %>
...
<% end %>

why my controller dont work since changing to postgres on rails 4?

I started developing my site in rails 4 with basically the database that comes with RoR sqlt3 and i want to take my site to production in a digital oceans server but they use postgres.. now I thought this was going to work the same way i have i started it. but now im getting errors
(Error)
PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type timestamp without time zone LINE 1: SELECT "videos".* FROM "videos" WHERE (created_at ) ORDER B... ^ : SELECT "videos".* FROM "videos" WHERE (created_at ) ORDER BY cached_votes_up DESC, created_at DESC
(Controller)
def index
#videos = Video.all.order("cached_votes_up DESC, created_at DESC")
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at ", Time.zone.now.beginning_of_day)
#items_by_day = #items.group_by { |t| t.created_at.beginning_of_day }
#topvideo = Video.order("cached_votes_up DESC, created_at DESC").where("created_at >= ?", Time.zone.now.beginning_of_day)
end
(View)
<% #items_by_day.sort.reverse.each do |day, items| %>
<div class="row">
<h1><%= day.strftime("%d %B %Y") %></h1>
<div class="topAd">
<center>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- responsive -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-1876888588409540"
data-ad-slot="6100356041"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>
</div>
<% for a in items %>
<div class="videothumb col-xs-12 col-sm-4">
<div class="" style="
background-size: 100% 100%;
height: 240px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
overflow: hidden;">
<%= link_to a do %>
<div style="background: rgba(0,0,0, 0); width:100%; height: 85%;z-index:1000; position:absolute; top:0;"></div>
<% end %>
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/<%= a.url %>" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-xs-12">
<h3>
<%= link_to a.title, a %> </h3>
</div>
<div class="col-xs-6">
<h2><%= a.artist %></h2>
</div>
<div class="col-xs-6">
<div style="text-align:right">
<%= link_to like_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-up">
<%= a.get_upvotes.size %>
<% end %>
<%= link_to dislike_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-down">
<%= a.get_downvotes.size %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
One of your where clauses is missing the assignment of its value for created_at.
I believe this line:
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at ", Time.zone.now.beginning_of_day)
should be replaced by:
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at >= ? ", Time.zone.now.beginning_of_day)

Resources