Why wash_out can't use identical data types in different soap actions? - ruby-on-rails

I use wash_out from the master branch.
Why I can't use identical data types in different soap actions?
Sample:
soap_action "get_groups",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
soap_action "get_items",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
Also I tried wrap it in WashOut::Type but it not help.
Error:
ActionView::Template::Error (Duplicate use of `data` type name. Consider using classified types.)

I found a solution for myself.
WashOut can't work with nested objects.
Every hash must be replaced with WashOut::Type.
It should look like this:
{:data => [SomeType], :total => :integer}

Related

Ruby on Rails Roo Import Excel Return NIL

So, I am developing an Ruby on Rails website to import an Excel file into our website. I followed RailsCasts and using Roo gem. All are working, but when the import process done, all fields are nil. When I checked with raise, I found out that the data is nil.
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
product = find_by_id(row["id"]) || new
product.attributes = row.to_hash.slice(*accessible_attributes)
product.save!
end
end
When I raised row, I got whole data from Excel. But when I raised product, I got nil. How I can put the data correctly so I can get all value correctly? Thank you.
So, after did some practices, I found out that on first test, it was a hash, so what I need to make it work is actually simple but I am not sure if this is the best practice. If someone have a better practice, it will be a good thing, but this is my solution for now.
object = Lead.new(
:first_name => row['First Name'], :last_name => row['Last Name'],
:address => row['Address'], :city => row['City'],
:state => row['State'], :county => row['County'],
:zip_code => row['Zip Code'], :bedrooms => row['Bedrooms'], :bathrooms => row['Bathrooms'],
:last_sale_date => row['Last Sale Date'], :amount_sold => row['Amount Sold'],
:tax_value => row['Tax Value'], :tax_name => row['Tax Name'], :tax_address => row['Tax Address'],
:tax_city => row['Tax City'], :tax_state => row['Tax State'], :tax_postal_code => row['Tax Postal Code'],
:listing_status => row['Listing Status'], :property_type => row['Property Type'],
:square_footage => row['Square Footage'], :days_on_market => row['Days on Market'], :list_date => row['List Date'],
:status_change_date => row['Status Change Date'], :year_built => row['Year Built'],
:mls_id => row['MLS ID'], :last_call_result => row['Last Call Result'], :last_dial_date => row['Last Dial Date'],
:last_contacted => row['Last Contacted'], :last_dial_time => row['Last Dial Time'], :create_date => row['Create Date'],
:edit_date => row['Edit Date'], :source => row['Source'], :list => row['List'],
:call_attempts => row['Call Attempts'], :family_member => row['Family Member'], :notes => row['Notes'],
:group => row['Group'], :manager => row['Manager']
)

Roo getting number instead of text

I am using roo to parse out an excel sheet like this
worksheet.parse(
:partNo => "PM_PartNo",
:salePartNo => "PM_SalesPartNo",
:appSearchInclude => "PM_AppSearchInclude",
:desc => "PM_WebApp_Description",
:brand => "PM_Brand",
:appSegment => "PM_ApplicationSegment",
:group => "PM_ProductGroup",
:design => "PM_ProductDesign",
:material => "PM_Material",
:line => "PM_ProdLine",
:baseSeries => "PM_BaseSeries",
:colorCode => "PM_ColorCode",
:series => "PM_Series",
:weightType => "PM_oz_gram",
:appRim => "PM_ApplicationRim",
:coating => "PM_Coating",
:pcs => "PM_PCSconversion",
:clean => true
) do |hash|
However, Roo keeps giving me a number 200275577.0 for the PM_PartNo column. In the excel sheet, this column has all cells formatted as text. What is should return in the parse is "200275577" as text, not 200275577.0 as a number. Is there a way to ensure it adheres to the excel formatting?
This is an open issue with roo. There is a general workaround contributed by a user in the issue, or you can just convert the value yourself with .to_i.to_s.

Searching with multiple conditions (Ruby on Rails)

I want to search a table with multiple conditions in Rails.
I am working on deleting certain package(record) from database but first i need to get userId and packageID .
and here is the code i wrote but it gives error.
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
here is the error :
ArgumentError in CreatePackagesController#deletepackage
Unknown key: condition
i just need equivalent code with the right syntax to that one if someone could help.
def deletepackage
#package=Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[ :user_id ]}
if (#package!=nil && req.receivedByCarrier==false)
#package.destroy
elsif (#package!=nil && req.receivedByCarrier==true)
#package.destroy
end
return;
end
Change your query as below:
#package = Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[:user_id]})
You are getting the error as Unknown key: condition because :condition is not a valid option in find method.
:condition should be :conditions (Note plural). Also, you should be passing both the conditions as a single key-value pair.
For Rails 4.x
You can simply do it as below
#package = Packages.where(id: params[:pid], senders_id: cookies[:user_id])
This
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
should be like this
#package=Packages.find(:all, :conditions => {:id => params[:pid]}, :senders_id => cookies[ :user_id ]})

Or conditions in mongoid

I have two queries,
Post.where(:group_id.in => group_ids, :deleted => false)
Post.where(:user_id=>user.id,:deleted=>false)
I need to combine these query using or condition.
I tried like,
Post.where(:deleted => false).or({:user_id=>user.id},{:group_id.in => group_ids})
and
Post.any_of({:group_id.in=>group_ids},{:user_id=>user.id})
but I didn't get results.
You can do it like that:
Post.any_of({:group_id.in => group_ids, :deleted => false}, {:user_id => user.id, :deleted => false})
or:
Post.all_of(:deleted => true, :or => [{:group_id.in => group_ids}, {:user_id => user.id}])
You can call selector on any of these expressions to see mongodb query that will be generated to fetch data.

Is it possible to know when an associated object is about to be destroyed?

When you have a one-to-many association in Rails 3 and accept nested attributes with delete, is it possible to know by looking at the objects (the associated object) whether it's going to be deleted or not?
For example:
group.attributes = {:member_attributes => {"0" => {:id => 1, :name => "John"},
"1" => {:id => 2, :name => "Dave"},
"2" => {:id => 3, :name => "Gus", "_destroy" => true}}}
Is it possible by looking at group.members to know that the one with id 3 is going to be deleted on save?
There's a method for finding that out, called marked_for_destruction?
group.members.each do |member|
puts "#{member.name} => #{member.marked_for_destruction?}"
end
would generate
John false
Dave false
Gus true

Resources