String Concatenation Sticking Point

5 views
Skip to first unread message

Brian Loomis

unread,
Nov 16, 2011, 4:22:50 PM11/16/11
to boi...@googlegroups.com
I've got a table in a legacy database that has some flattened SKU's for an order that are like so:
"WHPD3P1MOO;NWVIR10SAMPOO"

I'm splitting these and sending them to my product class to find out the product names so that I can replace the column of "WHPD3P1MOO;NWVIR10SAMPOO" in the csv output with ""PeakD3, Virulen" etc.

I'm doing mostly scripting with Ruby and have not worked with ActiveRecord much and this is probably a no brainer but I can't get my csv to not show these values in it for this column

"#<ActiveRecord::Relation:0x000001020b8670>, #<ActiveRecord::Relation:0x000001020b55d8>, #<ActiveRecord::Relation:0x000001020b33a0>"
"#<ActiveRecord::Relation:0x000001020af318>, #<ActiveRecord::Relation:0x000001020ad9c8>, #<ActiveRecord::Relation:0x000001020ab588>, #<ActiveRecord::Relation:0x000001020a8e50>, #<ActiveRecord::Relation:0x0000010209f850>"
"#<ActiveRecord::Relation:0x0000010209a800>, #<ActiveRecord::Relation:0x00000102098898>, #<ActiveRecord::Relation:0x00000102096980>"
"#<ActiveRecord::Relation:0x00000102091ef8>, #<ActiveRecord::Relation:0x0000010208f040>,


CSV.open("/_CLIENTFOLDER/mysql/export_orders.csv", "wb") do |csv|
      csv << ["Email", "Total", "First", "Last", "Items","Order Date"]
      @orders.each do |record|
        @producttotal=[]
        offerids=record['order_items'].split(/;/)
        offerids.each do |offerlookup|
          @offer = Offer.findproductoffer(offerlookup)
          @productname = Product.findproduct(@offer)
          @productname.to_s
          @producttotal.push(@productname.to_s)
        end
         # @#.join(', ')
        pp @producttotal.join(', ').to_s
         csv << [record['order_email'],
                record['order_total'],
                record['order_b_first'],
                record['order_b_last'],
                @producttotal,
                record['order_date']
            ]
      end
end


How can I adequately coerce the value returned from the product lookup and get it back to a string?

--
 
Brian Loomis

Alfie

unread,
Nov 16, 2011, 4:39:13 PM11/16/11
to Boise Ruby Brigade
I'm going to guess that:

@productname = Product.findproduct(@offer)
@productname.to_s
@producttotal.push(@productname.to_s)

ought to look more like:

@product = Product.findproduct(@offer)
@producttotal.push(@product.name)

Assuming that findproduct returns a Product object and that Product
has a name attribute that is a string.

Brian Loomis

unread,
Nov 16, 2011, 5:18:03 PM11/16/11
to boi...@googlegroups.com
That got me partway there.  I have strings now but no values.
"Product"
"Product"
"Product"
"Product"
"Product"
"Product"
"Product"
"Product, Product"
"Product, Product"
"Product, Product"
"Product"
"Product"
"Product"
"Product"
"Product"
"Product, Product"
"Product, Product, Product"
"Product, Product, Product, Product, Product"
"Product, Product, Product"
"Product, Product, Product"
"Product"
"Product, Product"
"Product, Product, Product"
"Product, Product"
"Product, Product"
"Product"
"Product, Product"
"Product, Product"
"Product, Product"
"Product, Product"
"Product, Product"
"Product"

Here's my Product class


class Product < ActiveRecord::Base
  set_table_name 'ncell_products'
  set_primary_key "prodpg_productid"
  has_many :ncell_product_offers, :foreign_key => :prod_offer_id, :primary_key => :prodpg_productid
  def self.findproduct(prodpg_productid)
    
    where( :prodpg_productid => prodpg_productid).select("prodpg_title")
  end
end

--
 
Brian Loomis

--
You received this message because you are subscribed to the Google Groups "Boise Ruby Brigade" group.
To post to this group, send email to boi...@googlegroups.com.
To unsubscribe from this group, send email to boiserb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/boiserb?hl=en.

Alfie

unread,
Nov 16, 2011, 6:35:44 PM11/16/11
to Boise Ruby Brigade
What's the schema look like for Product? What attribute/column are you
trying to return? If you run Product.first in the console, what does
that give you?

Brian Loomis

unread,
Nov 16, 2011, 8:21:54 PM11/16/11
to boi...@googlegroups.com
 => #<Product prodpg_productid: 1, prodpg_title: "Q - #1", prodpg_link: "q--1", prodpg_varcount: 1, prodpg_discount: 0.0, prodpg_manufacturerid: 0, prodpg_featured: 0, prodpg_bestseller: 0, prodpg_minprice: 0.0, prodpg_minretail: 0.0, prodpg_forsale: "N", prodpg_descr: "Dr. William V. Judy personally...", prodpg_fulldescr: "<ul>\r\n<li>Significantly slows aging by helping to p...", prodpg_thumbnail: "q_bottle.jpg", prodpg_fullsize: "q_bottle.jpg", prodpg_seo_keywords: "", prodpg_search_tags: "", prodpg_meta_keywords: "", prodpg_meta_descr: "", prodpg_dt_added: "2008-09-05 12:59:26", prodpg_dt_modified: nil, prodpg_has_tax: 0, prodpg_purg_id: nil, prodpg_template: "productinfo", prodpg_nativeid: nil, prodpg_simplename: nil, prodpg_size: nil, prodpg_vendor: nil, prodpg_salerank: nil, prodpg_overstock: nil, prodpg_important: 0, prodpg_features: nil, prodpg_bestdeal: nil, prodpg_details: nil, prodpg_supplements: nil, prodpg_information: nil, prodpg_benefits: nil, prodpg_text_details: nil, prodpg_text_supplements: nil, prodpg_text_information: nil, prodpg_text_benefits: nil, prodpg_dt_posted: nil, prodpg_upcharge: nil, prodpg_has_bad_descr: false, prodpg_top_pos: 0, prodpg_need_descr: false, descr_src: false> 

CREATE TABLE `ncell_products` (
  `prodpg_productid` int(11) NOT NULL AUTO_INCREMENT,
  `prodpg_title` varchar(255) NOT NULL DEFAULT '',
  `prodpg_link` varchar(200) DEFAULT '',
  `prodpg_varcount` int(5) DEFAULT '1',
  `prodpg_discount` float DEFAULT '0',
  `prodpg_manufacturerid` int(11) DEFAULT '0',
  `prodpg_featured` int(2) DEFAULT '0',
  `prodpg_bestseller` int(2) DEFAULT '0',
  `prodpg_minprice` float DEFAULT '0',
  `prodpg_minretail` float DEFAULT '0',
  `prodpg_forsale` varchar(1) NOT NULL DEFAULT 'Y',
  `prodpg_descr` text,
  `prodpg_fulldescr` text,
  `prodpg_thumbnail` varchar(128) DEFAULT NULL,
  `prodpg_fullsize` varchar(128) DEFAULT NULL,
  `prodpg_seo_keywords` varchar(255) DEFAULT '',
  `prodpg_search_tags` varchar(255) DEFAULT '',
  `prodpg_meta_keywords` varchar(255) DEFAULT '',
  `prodpg_meta_descr` varchar(255) DEFAULT '',
  `prodpg_dt_added` datetime DEFAULT NULL,
  `prodpg_dt_modified` datetime DEFAULT NULL,
  `prodpg_has_tax` int(2) DEFAULT '0',
  `prodpg_purg_id` varchar(40) DEFAULT NULL,
  `prodpg_template` varchar(32) DEFAULT NULL,
  `prodpg_nativeid` char(30) DEFAULT NULL,
  `prodpg_simplename` char(100) DEFAULT NULL,
  `prodpg_size` char(30) DEFAULT NULL,
  `prodpg_vendor` int(11) DEFAULT NULL,
  `prodpg_salerank` int(11) DEFAULT NULL,
  `prodpg_overstock` int(11) DEFAULT NULL,
  `prodpg_important` int(10) NOT NULL DEFAULT '0',
  `prodpg_features` blob,
  `prodpg_bestdeal` int(11) DEFAULT NULL,
  `prodpg_details` int(11) DEFAULT NULL,
  `prodpg_supplements` int(11) DEFAULT NULL,
  `prodpg_information` int(11) DEFAULT NULL,
  `prodpg_benefits` int(11) DEFAULT NULL,
  `prodpg_text_details` text,
  `prodpg_text_supplements` text,
  `prodpg_text_information` text,
  `prodpg_text_benefits` text,
  `prodpg_dt_posted` datetime DEFAULT NULL,
  `prodpg_upcharge` int(11) DEFAULT NULL,
  `prodpg_has_bad_descr` tinyint(1) NOT NULL DEFAULT '0',
  `prodpg_top_pos` int(11) DEFAULT '0',
  `prodpg_need_descr` tinyint(1) NOT NULL DEFAULT '0',
  `descr_src` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`prodpg_productid`),
  KEY `fi` (`prodpg_forsale`,`prodpg_productid`),
  KEY `mid` (`prodpg_manufacturerid`)
) ENGINE=MyISAM AUTO_INCREMENT=112 DEFAULT CHARSET=utf8

--
 
Brian Loomis

Alfie

unread,
Nov 16, 2011, 8:55:48 PM11/16/11
to Boise Ruby Brigade
I *think* something like this should get you what you want...

...
offerids.each do |offerlookup|
@offer = Offer.findproductoffer(offerlookup)
@product= Product.find_by_prodpg_productid(@offer) # this
assumes that @offer returns an integer of the id you're looking for
@producttotal << @product.prodpg_title
end
...

Brian Loomis

unread,
Nov 17, 2011, 9:46:00 AM11/17/11
to boi...@googlegroups.com
This is what I ended up doing. There were a lot of inconsistencies in the old data that made this more difficult that I assumed it would be.

class Product < ActiveRecord::Base
  set_table_name 'ncell_products'
  #set_primary_key "prodpg_productid"
  has_many :ncell_product_offers, :foreign_key => :prod_offer_id, :primary_key => :prodpg_productid
  def self.findproduct(prodpg_productid)
  
  #self.find(prodpg_productid)
    where( :prodpg_productid => prodpg_productid).select("prodpg_title")
  end
  
  def self.getproduct(prodpg_productid)
  
  self.find(prodpg_productid)
  
  end
end

f = File.open('email_un.txt')
a = f.readlines
@aa=a.each { |line|
    line.chomp!
    }

@orders =  Order.findorder(@aa)
@customers = Customer.findcustomer(@aa)

offerids="WHPD3P1MOO;NWVIR10SAMPOO".split(/;/)
offerids.each do |offerlookup|
  @offer = Offer.findproductoffer(offerlookup)
  @productname = Product.findproduct(@offer)

end

CSV.open("/_NUTRICELL/mysql/export_orders.csv", "wb") do |csv|
      csv << ["Email", "Total", "First", "Last", "Items","Order Date"]
      @orders.each do |record|
        @producttotal=[]
        unless record['order_items'] == nil
        offerids=record['order_items'].split(/;/)
        offerids.each do |offerlookup|
          @offer = Offer.findproductoffer(offerlookup)
          unless @offer.size == 0
            @offer.each do |thisoffer|
            @product = Product.findproduct(thisoffer.prod_offer_prodpg_id)
            @productname = Product.find_by_prodpg_productid(thisoffer.prod_offer_prodpg_id)
            unless @productname == nil
            @producttotal.push(@productname.prodpg_title.chop!)
            end
          end
         end
        end
      end
      
 
    csv << [record['order_email'],
          record['order_total'],
          record['order_b_first'],
          record['order_b_last'],
          @producttotal.join(', ').to_s,
          record['order_date']
      ]
  end
end

--
 
Brian Loomis

Reply all
Reply to author
Forward
0 new messages