Sascha M.

Joined

1,210 Experience
1 Lesson Completed
0 Questions Solved

Activity

Posted in Loop through associated records and deliver as csv

Hi Chris,

many many thanks! I'll try this out.

Posted in Loop through associated records and deliver as csv

Hi Chris,

thank you for your reply. I wasn't aware of the markdown possibility. Thanks for the tip.

Actually I just want to export some fields of the 2 seperate tables (Sales and Orders) in one file - without spreadsheets.

Just like that I have

id, user_id, user_name, brand, customer_id, product_group, product, quantity, price, setup, discount

as the column header. The first part (id, user_id, user_name, brand, customer_id) will come from the Orders table and the second part (product_group, product, quantity, price, setup, discount) from the Sales table. I know that then the first part from the orders table gets duplicated each time when one order has more than 1 Sale, but that's ok. So the result should look like this (first 2 got the same order_id, as one customer has bought 2 shoes within one order):

id, user_id, user_name, brand, customer_id, product_group, product, quantity, price, setup, discount
1, 10, Sascha, Adidas, 35678, Shoes, Shoe No. 1, 1, 49.99, 0, 0
1, 10, Sascha, Adidas, 35678, Shoes, Shoe No. 2, 1, 69.99, 0, 0
2, 10, Sascha, Adidas, 35699, Shoes, Shoe No. 3, 1, 79.99, 0, 0

Hope that my questions is clearer now! :)

Many thanks,
Sascha

Posted in Loop through associated records and deliver as csv

No Ideas? :(

Posted in Loop through associated records and deliver as csv

Hi there,

I've watched your CSV export episode and I'm using the exact same code to export the records to a CSV.

I've got two tables: Orders and Sales. 1 Order has_many Sales and 1 Sale belongs_to an Order. The Orders table is the "parent" of the Sales table. So the 'id' of the table Orders is the same as the 'order_id' in the Sales table. This is how I match a Sale to an Order. Just like a blog article with comments.

Here is an example of the sales csv export code:

def self.to_csv
    attributes = %w{id product_group quantity product price setup discount order_id}
    CSV.generate(headers: true) do |csv|
      csv << attributes
      all.each do |sale|
        csv << sale.attributes.values_at(*attributes)
      end
    end
  end

Here is an example of the orders csv export code:

def self.to_csv
    attributes = %w{id user_id user_name brand customer_id}
    CSV.generate(headers: true) do |csv|
      csv << attributes
      all.each do |order|
        csv << order.attributes.values_at(*attributes)
      end
    end
  end

Both exports are perfectly fine but it's a mess to connect the two tables again in excel. As the 'id' of the orders table is the same as the 'order_id' in the sales table I thought there should be a way to export all columns of both tables combined in just one csv file. Is that possible?

MANY MANY thanks in advance!

Posted in How to raise a warning message from the model

Hi there,

In my rails app I have the parent model ORDERS with SALES as a child (as nested routes). So 1 Order has_many Sales and 1 Sale belongs_to Order. In my USER model I have the column "maxdiscount". This is the place where a manager types in a value, like 100 $.

Sales agents can give a discount on a SALE. To check if the entered value from the sales agent is not higher than the threshold defined in the maxdiscount I did this:

def discount=(discount)
 if discount.present?
   if current_user.maxdiscount >= discount.to_d
     discount = discount.gsub(",", ".") #gsub for changing comma-seperated values into dot-seperated.
     self[:discount] = discount
   end
 end
end

The check works as intended, but when the if-clause is true like the sales agent typed in a value higher than his maxdiscount threshold then the discount does not get saved. Which is kind of ok.. What I want to do is to raise a warning with a flash message but this does not work. So far I understood that you can't raise error messages from the model.
This is what I did, but that is not working :/

 def discount=(discount)
  if discount.present?
    if current_user.maxdiscount >= discount.to_d
     discount = discount.gsub(",", ".")
     self[:discount] = discount
    else
      self.errors.add(:base, "The discount is too high. The product was added without it.")
     end
   end
  end

Any suggestions? Thanks in advance!

Posted in GoRails Screencast suggestion: Caching

Thanks for that. I'll give it a try.

Posted in GoRails Screencast suggestion: Caching

Hi there,

just a quick suggestion. I'm at a stage where I'm struggling with Caching. Since you haven't covered this topic yet I would be very happy if you do, especially Database query caching.

Greetings!

Hi Chris,

I have this in my controller:

def import
@import = Warehouse::Import.new warehouse_import_params
if @import.save
redirect_to warehouses_url, notice: "Imported #{@import.imported_count} products."
else
@warehouses = Warehouse.all
flash[:alert] = "There were #{@import.errors.count} errors with your CSV file."
render action: :index
end
rescue
redirect_to warehouses_url, notice: "No CSV :("
end

As it loops through until the column with the blankspace it only gives me an "No CSV" error. Strangely it is the blankspace. As soon as i deleted it everything did work fine. :/

Hi there,

I`m using the CSV Upload as described in one of your pro-tutorials.

The code loops through the csv and adds records to the specific table. But if there is a whitespace inside a row, the upload get's stuck.

Column head:
category,product,producttype,brand,price
Shoes,Adidas Test Shoes ,Base,Adidas,43.99

Notice the blank behind "Adidas Test Shoes". This produces the error. What can be done here?

This is the import code:

class Warehouse::Import
include ActiveModel::Model
attr_accessor :file, :imported_count

def process!
@imported_count = 0
CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
warehouse = Warehouse.assign_from_row(row)
if warehouse.save
@imported_count += 1
else
errors.add :base, "Line #{$.} - #{warehouse.errors.full_messages.join(",")}"
end
end
end

def save
process!
errors.none?
end
end

Posted in CSV Import

Great. Thank you for your detailed answer! I Just saw, that "order_id" actually is named "reference code" with a space. How do I need to write it :ReferenceCode, or :reference_code ?

Posted in CSV Import

My question is about this Episode: https://gorails.com/episodes/upload-csv-form

The CSV Import works fine (I'm using the exact same code actually).

My 1. Question - How can I define an alias for my rows?

This is the code:

def self.assign_from_row(row)
order = Order.where(token: row[:token]).first_or_initialize
order.assign_attributes row.to_hash.slice(:token, :order_id, :customer_id)
order
end

How would you implement aliases for token (utm_campaign) order_id (ordernumber) and customer_id (customer)? It's annoying to change the row names each time.

My 2. Question - Accept Semicolons as a Divider

In Germany it is common to use a ";" as a divider inside a .csv. US Standard is a comma. What should be edited so both is possible?

Many Thanks in Advance!

Posted in CanCanCan: Defining Abilities

I'm using the CanCanCan gem. According to the documentation you can define abilities. That is working for me. What I want to do is to limit the access to records, that contain a value. Something like:

"can :crud, Order, :brand == nil"

is not working

A User should only be allowed to see the record, if the brand-column of the Order table is empty. As my example doesn't work apparently.. How would you do it?

Posted in Add Download Button to csv-export

thx Jamie, that worked well!

Posted in Add Download Button to csv-export

Hi,
I did everything (and it's working!) regarding this episode:
https://gorails.com/episodes/export-to-csv?autoplay=1

How can I add a download-button in my views for that?

Thank you guys!

Posted in Adding a Admin Interface to devise

Hi there,

first of all: I'm pretty new to Rails, so please don't blame me if I ask a stupid question :)

I use devise + cancancan for authentication and authorization. The user model is already generated by devise (inluding the views). Now I want to disable public registrations. Only admins (cancancan role is admin) should be able to create and edit users (e.g. list of users with edit button). How would your approach look like? I'm totally lost with that :(