Skip to content

Instantly share code, notes, and snippets.

@ornerymoose
ornerymoose / relationship.rb
Created December 4, 2017 20:16
So this 'works', in a sense: it's doing the UPDATE but also doing an INSERT. Why is that? Screenshot of server log: https://imgur.com/a/8FCPN
class Relationship < ApplicationRecord
belongs_to :outage
belongs_to :child
validate :outage_changed, if: :outage_id_changed?
private
def outage_changed
Relationship.where(child_id: self.child_id).update_all(outage_id: self.outage_id)
@ornerymoose
ornerymoose / child.rb
Last active December 1, 2017 02:14
I need the association model (relationship.rb) to be separate from child and outage, bc child and outage will connect to two separate remote SQL Server databases that will just read data. Where the relationship model will make the association: child belongs_to outage, outage has_many children
class Child < ApplicationRecord
end
@ornerymoose
ornerymoose / child.rb
Last active November 24, 2017 16:48
Given the custom validation in relationship.rb, if I try to update an outage with a child that is already associated to a different outage, it gives me a validation error. It's trying to create a new relationship, not update one. If I pass the on: :create to the custom validation, that won't work since it's trying to create a new relationship on…
class Child < ApplicationRecord
has_many :relationships
has_many :outages, through: :relationships
end
@ornerymoose
ornerymoose / show.html.erb
Last active November 21, 2017 19:13
undefined method `id' for nil:NilClass. Works when I remove the map and just do Child.all for the collection
<%= collection_check_boxes(:outage, :child_ids, Child.all.map{|c| c.omnia_trouble_ticket if !c.relationships.present?}, :id, :omnia_trouble_ticket) do |b| %>
@ornerymoose
ornerymoose / show.html.erb
Last active November 20, 2017 22:07
ok this worked in outages/show.html.erb. Had to add in @relationship = Relationship.new to the outage#new action.
<%= form_for @relationship, :url => relationships_path do |f| %>
<%= f.text_field :child_id %>
<%= f.text_field :outage_id %>
<%= f.submit %>
<% end %>
class Outage < ApplicationRecord
has_many :relationships
has_many :children, through: :relationships
end
class Child < ApplicationRecord
has_many :relationships
has_many :outages, through: :relationships
end
class Child < ApplicationRecord
has_one :outage, through: :relationship
has_one :relationship
end
@ornerymoose
ornerymoose / model.rb
Created September 20, 2017 19:05
if i remove rows on localhost:3000 via jquery, those removed rows aren't removed when generating the CSV. Why?
def generate_csv
url = "http://localhost:3000"
doc = Nokogiri::HTML(open(url, :allow_redirections => :safe))
csv = CSV.open("#{Rails.root}/public/collections-output.csv", 'w',{:col_sep => ","})
csv << ['HEADERS HERE']
doc.xpath('//table/tbody/tr').each do |row|
tarray = []
row.xpath('td').each do |cell|
tarray << cell.text
end
@ornerymoose
ornerymoose / controller.rb
Last active August 31, 2017 16:45
line 11: File.size comes back as not equal to 0 (which is good) in Rails Console, but returns 0 when run in the below method
def sum_close
daily_closed_tickets = Fst.sum_retrieve_closed_tickets
daily_closed_tickets.each do |ticket|
CSV.open("/var/www/html/harmonize/public/close/CLOSED_#{ticket.attributes['TroubleTicketNumber']}_sum.txt", "w+", {force_quotes: false}) do |csv|
if (ticket.attributes['ClosedDate'].present? && ticket.attributes['CauseCode'].present? && (FileCopyReceipt.exists?(path: "/var/www/html/harmonize/public/open/OPEN_#{ticket.attributes['TroubleTicketNumber']}_sum.txt")))
csv << ["GENERATE CSV WITH ATTRIBUTES HERE"]
files = Dir.glob("/var/www/html/harmonize/public/close/CLOSED_#{ticket.attributes['TroubleTicketNumber']}_sum.txt")
files.each do |f|
Rails.logger.info "CHECKING SUM PATH (should be false): #{FileCopyReceipt.exists?(path: f)}"
Rails.logger.info "File size (should return non-0): #{File.size(f)}" #returns 0, but not in Rails Console
USE [OMNIA_ESUM_P_SUM_CM]
GO
/****** Object: StoredProcedure [dbo].[SUM_Harmonize_Clear_SP] Script Date: 8/30/2017 10:28:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO