Having some real problems with this and hoping someone can help. Here's a reduced sample of the data I'm working with. So here $order_info is an array, inside that array theres another array called $items and within that there's multiple arrays which have the item ID as a name (very annoying!). What I'm trying to do is loop through the arrays within the $items array - the problem is I won't know their name. Is it possible to just loop through every array within an array (which is also within an array!)? It also needs to use smarty. I've tried various nested for loops but all have just come back with a blank page, suggesting an error.
{$order_info} Array (86)
order_id => "15"
items => Array (2)
4008222099 => Array (19)
item_id => "4008222099"
order_id => "15"
product_id => "836"
product_code => "B0001WS6L2"
price => "229.95"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "LINGO TR-2203 Pacifica Talk Talking T..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "229.95"
stored_price => "N"
product => "LINGO TR-2203 Pacifica Talk Talking T..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "229.95"
original_price => "229.95"
cart_id => "4008222099"
tax_value => "0"
subtotal => "229.95"
display_subtotal => "229.95"
shipped_amount => "0"
shipment_amount => "1"
1157311813 => Array (19)
item_id => "1157311813"
order_id => "15"
product_id => "744"
product_code => "B00028DM96"
price => "119.99"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "Sharp Electronics PW-E550 Electronic ..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "119.99"
stored_price => "N"
product => "Sharp Electronics PW-E550 Electronic ..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "119.99"
original_price => "119.99"
cart_id => "1157311813"
tax_value => "0"
subtotal => "119.99"
display_subtotal => "119.99"
shipped_amount => "0"
shipment_amount => "1"
The following solved it - should help if people need to do the same in the future:
{foreach from=$order_info.items item=foo}
{$foo.product_id}
{/foreach}
Related
I am currently trying to build a table to display a list of users with their attributes in separate columns.
it is something like:
user1 | user1 email | user1 admin role | user1 organiser role
user2 | user2 email | user2 admin role | user2 organiser role
user3 | user3 email | user3 admin role | user3 organiser role
However, the admin and organiser attributes are stored in a hstore column called activities.
When I build the datatable the browser returns this error
DataTables warning: table id=DataTables_Table_0 - Requested unknown
parameter 'activities.organiser' for row 0. For more information about
this error, please see http://datatables.net/tn/4
This is my code where I build my table columns
in manage_user_controller.rb
private
def load_columns
columns = []
columns << {:data => "id", :title => "ID"}
columns << {:data => "email", :title => "Email"}
columns << {:data => "created_at", :title => "Created"}
columns << {:data => 'activities.organiser', :title => "Organiser Role"}
columns << {:data => 'activities.admin', :title => "Admin Role"}
columns << {:data => "id", :title => ""}
return columns
end
in my users_datatable.rb
private
def data
results = []
users.each_with_index do |user, index|
cols = {"id" => "#{user.id}"}
cols.merge!("email" => "#{user.email}")
cols.merge!("created_at" => "#{user.created_at}")
cols.merge!("activities" => "#{user.activities.organiser}")
cols.merge!("activities" => "#{user.activities.admin}")
#cols.merge!("activities" => "#{user.activities.admin_events}")
results << cols
end
return results
end
I have tried to change "activities.organiser" to "activities" in the controller, it will load some values, but I cant possible use activites for all the sub attributes in activites itself because that will be wrong.
So, how should I correctly populate a datatable with key-value pairs from hstore column?
Thank you very much.
ok, i manage to solve it.
in the manage_users_controller.rb
def load_columns
columns = []
columns << {:data => "id", :title => "ID"}
columns << {:data => "email", :title => "Email"}
columns << {:data => "created_at", :title => "Created"}
columns << {:data => "organiser", :title => "Organiser Role"}
columns << {:data => "admin", :title => "Admin Role"}
columns << {:data => "id", :title => ""}
return columns
end
In the user_datatable.rb
def data
results = []
users.each_with_index do |user, index|
cols = {"id" => "#{user.id}"}
cols.merge!("email" => "#{user.email}")
cols.merge!("created_at" => "#{user.created_at}")
cols.merge!("organiser" => "#{user.activities.organiser}")
cols.merge!("admin" => "#{user.activities.admin}")
#cols.merge!("activities" => "#{user.activities.admin_events}")
results << cols
end
return results
end
the names must be the same in both files.
This should be an easy one but I am not getting it right. Looked around on Google and SO and found a few suggestions but can't get any of them to work for me.
I need to output a specific data structure which is an array of hashes that contains an index for each hash:
[ id => {:key=>"foo", :value=>"bar"}]
I have a Class object ("Foo"), that I need to map into the above structure. I have come this far:
[{:key=>"personal", :value=>"age"}]
...Using this Code:
check = Foo.find(1)
check.collect{|r| {:key => r.name, :value => r.type}}
What I'd like to achieve is to add an index to the hash above. The index's value should be "check.id".
I think that this might be heading in the right direction but not sure (Results in an error):
check.group_by {|r| r.id }.collect{ |r| {:key => r.name, :value => r.type}}
This should work
check.collect{|r| {r.id.to_s => {:key => r.name, :value => r.type}}}
Actually I assume you want to get hash of hashes:
Hash[ *check.collect { |r| [r.id.to_s, {:key => r.name, :value => r.type}] }.flatten ]
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 ]})
I have 34 elements, but I only need to know which are the top 5 ranked.
I am using a comparison "A versus B" type questionaire across all 34 elements.
Right now, it isn't dynamic, so I cannot change the questions based on the prior questions.
What is the minimum number of questions in the questionaire I can ask?
What is the sorting algorithm you would use?
Right now I am using manualation and using Excel, but want to know how to eventually do it in Ruby for a basic Rails app.
If you just have simple integers, floats or strings, you can sort an array easily:
a = [ 3,3,1,6,2,8 ]
a.sort # => [1,2,3,3,6,8]
But this sorts ascending.
To sort descending, you can do:
a.sort {|x,y| y <=> x } # => [8,6,3,3,2,1]
or
a.sort.reverse # => [8,6,3,3,2,1]
Then to get the first 5 elements, just use:
a.sort.reverse.take(5) # => [8,6,3,3,2]
If the elements you are sorting are actually structures, you can change the code inside a sort_by block to account for that easily, eg:
a = [{:score => 5, :name => "Bob"}, {:score => 51, :name => "Jane"}, \
{:score => 15, :name => "Joe"}, {:score => 23, :name => "John"}, \
{:score => 35, :name => "Sam"}, {:score => 1, :name => "Rob"}]
a.sort_by{|x| x[:score] }.reverse.take(5)
# => [{:score => 51, :name => "Jane"},{:score => 35, :name => "Sam"},...]
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