last day (14 days later) » 

3:11 PM
1
A: Get newest child on parent.all in one call - Rails 4

Arup RakshitYou need to write the query as: Order .joins(:order_status_details) .order('id DESC') .where("order_status_details.status_id = max(order_status_details.status_id)")

 
Sorry about this, the scope is Order model... is not related with status. Check the update please
 
@AlejandroAraujo I updated.
 
Sorry but Im still seeing the same :/
 
@AlejandroAraujo I had a typo, now please check.
 
Im gettin this error bro: Mysql2::Error: Unknown column 'status_id' in 'where clause': SELECT COUNT(*) FROM orders WHERE (status_id = max(status_id)) The table above is order_status_details, not orders :(
 
3:11 PM
@AlejandroAraujo please include the schema.rb content for the 2 models.
@AlejandroAraujo try now..
 
Done, please the schema. It can't be OrderStatusDetails because I need a Orders list (All) with the most recent status inside.
 
@AlejandroAraujo see now..
 
Something like: Order.all.order('id DESC') OrderStatusDetail.order('created_at DESC').limit(1).first combined, need more details? Please let me know
 
@alej hi
 
Hi bro
@ArupRakshit
 
3:14 PM
did you try my last query ?
 
Yes, I got this error
Mysql2::Error: Invalid use of group function: SELECT COUNT(*) FROM orders INNER JOIN order_status_details ON order_status_details.order_id = orders.id WHERE (order_status_details.status_id = max(order_status_details.status_id))
 
wait
 
@AlejandroAraujo try now
Mysql sucks, if it is Postgresql my old answer would work
 
Why is better?
 
3:23 PM
postgresql very flexible.. lot of feature it has which Mysql don't have
postgres very helpful .. and less effort needs
 
Any tutorial recommended?
I got no error but the result was the following: {"id"=>7, "customer_id"=>8, "subtotal"=>2990, "tax"=>358, "total"=>2332, "invoice"=>nil, "shipping_id"=>nil, "created_at"=>Sat, 30 Jan 2016 23:31:21 VET -04:30, "updated_at"=>Sat, 30 Jan 2016 23:31:21 VET -04:30, "comission"=>299, "comment"=>nil}
I think that I did with this, check
@orders = Order.all.order('id DESC')
<%= order.order_status_details.last.status_id %>
 
is it not working you meant ?
 
Yes, check the JSON... I cant see any status data
But, with my solution I can print the last ID from order_status_details
But now I have another problem: :'(
 
what data you want in the result ?
don't you need only the order columns ?
 
My solution have the display the most recent status name
It possible to display the most recent status id, but not the name
have to display*
 
3:27 PM
ok
 
How can I get the status name using the ID?
 
so you want only this columns ?
id | status_id | created_at | updated_at | order_id
in the result ?
 
Yes, inside each order
Remember, Im using @orders = Order.all.order('id DESC')
 
you will be using my query, there , not the one you have.. and to the query I gave just like below
 
Let me show you the output using your query
 
3:31 PM
Order
 .joins(:order_status_details)
 .order('id DESC')
 .where(
    "order_status_details.status_id = \
     (SELECT max(status_id) FROM order_status_details)"
  ).select("order_status_details.id, status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.update_at, order_status_details.order_id")
now, if you want to get status name, make 1:1 relationship between order_status_details and order_statuses
 
Lol now I dont have the order data
With that
 
do you use codementor ? we can share screen..
I think, I need to see your code and data.. either of us misunderstanding.. :/
 
I think that I have a codementor account
Let me check
Yes, what I have to send you?
 
@AlejandroAraujo the code above seems ok.. what do you mean by no order data
?
 
Yes, just the order status data...
But I want the keep the order data.... Subtotal, comission, tax, total, etc
 
3:44 PM
and you want order data also with that ?
 
want to keep*
 
ok. add there orders.* in select()
 
If I show a JSON example, that help?
With the structura that I want
structure*
 
yes, but did you add what i said ?
 
Let me try
I have 2 selects
Order.joins(:order_status_details).order('id DESC').where("order_status_details.status_id = \ (SELECT max(status_id) FROM order_status_details)").select("order_status_details.id, order_status_details.status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.updated_at, order_status_details.order_id")
SELECT and .select
 
3:47 PM
that SELECT is inner query.. it not there to show you data
.select(..) is what give the actual result
 
Like this?
Order.joins(:order_status_details).order('id DESC').where("order_status_details.status_id = \ (SELECT max(status_id) FROM order_status_details)").select("orders.*, order_status_details.id, order_status_details.status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.updated_at, order_status_details.order_id")
 
Got this error
Mysql2::Error: Column 'id' in order clause is ambiguous: SELECT orders.*, order_status_details.id, order_status_details.status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.updated_at, order_status_details.order_id FROM orders INNER JOIN order_status_details ON order_status_details.order_id = orders.id WHERE (order_status_details.status_id = (SELECT max(status_id) FROM order_status_details)) ORDER BY id DESC
Fixed, adding 'as detail_id'
 
:o
Very very close
{"id"=>7, "customer_id"=>8, "subtotal"=>2990, "tax"=>358, "total"=>2332, "invoice"=>nil, "shipping_id"=>nil, "created_at"=>Sun, 31 Jan 2016 00:31:21 VET -04:30, "updated_at"=>Sun, 31 Jan 2016 00:31:21 VET -04:30, "comission"=>299, "comment"=>nil, "detail_id"=>5, "status_id"=>3, "order_id"=>7}
this is what I was talking about
 
3:53 PM
and what you got ?
 
This is ok, but now I have the status_id... check the key on the JSON
Its possible to translate that ID into status name, right?
 
For example, status_id 3 means 'Shipped' on order_status table
 
as I said,
now, if you want to get status name, make 1:1 relationship between order_status_details and order_statuses
 
Why 1:1? Is it not a 1:many relation?
1 order_status could have many order_status_details
 
3:57 PM
why ?
ok yes
1 to many
 
order_statuses is a list of all the possible statuses
 
hm
1 min
 
order_status_details is like a log but the status history of the order
with the status*
 
can you paste query here ?
I will edit there...
 
The query result?
 
3:59 PM
no the actual query
 
Sure
SELECT orders.*, order_status_details.id as detail_id, order_status_details.status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.updated_at, order_status_details.order_id FROM orders INNER JOIN order_status_details ON order_status_details.order_id = orders.id WHERE (order_status_details.status_id = (SELECT max(status_id) FROM order_status_details)) ORDER BY id DESC;
 
Hahahaa
 
I meant Rails query that I gave you with modification
 
Oh sorry, ok
 
4:00 PM
quick, I will go for dinner
 
joins(:order_status_details).order('id DESC').where("order_status_details.status_id = \ (SELECT max(status_id) FROM order_status_details)").select("orders.*, order_status_details.id as detail_id, order_status_details.status_id, order_status_details.status_id, order_status_details.created_at, order_status_details.updated_at, order_status_details.order_id")
 
please show me the model relationship between those 2 models..
 
class OrderStatus < ActiveRecord::Base
has_many :order_status_details
end

class OrderStatusDetail < ActiveRecord::Base
belongs_to :order_status, foreign_key: :status_id
belongs_to :order, foreign_key: :order_id
end
 
4:03 PM
ok
change the old join as joins(:order_status_details: : order_status) and add in the selec one more caluse like order_statuses.name
 
I do not understand what are you trying to say about the join
 
you now have joins(:order_status_details).
replace it with what I said
 
with joins(:order_status_details: : order_status) or joins(:order_status_details :order_status)?
joins(:order_status_details: : order_status) is wierd
 
first one joins(order_status_details: : order_status)
I had a typo
first one joins(order_status_details: :order_status)
 
DONE!
AWESOME! :O
 
4:08 PM
DUDE! .. what effort I gave .. in free :p
 
You are a god bro :)
Do you have any social account to follow you? :) I promise that I wont annoy you
 
now accept my answer.. :)
 
Change your solution on the question the mark as correct
 
done!
 
Done :)
tyvm!
 
4:10 PM
you are from ?
 
Venezuela
Lol
 
ok,, learning rails ?
 
We have problems with the USD$
 
or working on a paid project ?
np.. i also do have .. :/
 
I can not pay because of that
This is like Cuba but we have internet lol
 
4:11 PM
np.. :)
forget it.
 
No no, learning rails but
Its a personal project too
 
i will be back after 1 hour... need to grab some food..
ttyl
I am on facebook
 
Something like a ERP app for a personal business
 
ok, you can see my linkedin attached with my SO profile
 
Im pro but using php and laravel hahaha
 
4:12 PM
we can connect there.. if you want
brb 1 hour.. if you need ever rails dev to hire.. give me a shout
 
Sure! send me your profile to find you when you can... and go for your dinner :)
Sure ;) tyvm
 

last day (14 days later) »