Reputation: 627
I'm trying to pass the variable supplier
into the where clause of my sql statement. It saves the variable as when i click the link it shows the chosen supplier in the url
. I just have no idea what to put in the statement to make it pick up the variable from the view.
Model:
def self.find_supplier
find_by_sql(["SELECT s.supplier, s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s
LEFT JOIN licenses l ON s.id=l.software_id
WHERE s.supplier = ? AND l.amount > 0
GROUP BY s.supplier, s.vendor, s.title, s.edition", :supplier)
end
View:
<% @softwares.each do |l| %>
<li>
<%= link_to "#{l.supplier}", :controller =>'softwares', :action => 'export_supplier', :supplier => l.supplier %>
</li>
<% end %>
Controller:
def export_supplier
@software = Software.find_supplier
software = CSV.generate do |csv|
# for the headers of the csv file
csv << ["Quantity", "Item Number", "Item Description"]
# the chosen rows from the database
@software.each do |s|
csv << [s.amount, s.productcode, s.description, s.supplier]
end
end
send_data(software, :type => 'text/csv', :filename => 'software.csv')
end
Upvotes: 2
Views: 1019
Reputation: 1562
find_by_sql(["SELECT s.supplier, s.productcode, s.description, CAST(SUM(l.amount) as UNSIGNED) AS amount
FROM softwares s LEFT JOIN licenses l ON s.id=l.software_id WHERE s.supplier = ? AND l.amount > 0 GROUP BY s.supplier, s.vendor, s.title, s.edition", params[:supplier])
I assume the find_by_sql is in your controllers, "export_supplier" method. Here you should get params[:supplier]
Upvotes: 1