How to use `after_update' callback in rails?
up vote
0
down vote
favorite
I want to use after_update callback method for a method, which updates the product's stock in shopping cart application. The code in controller is:
ProductsController.rb:
def update
order = current_order
@order_item = order.order_items.find(params[:id])
@order_item.product.save
end
And in ProudctModel:
after_update :remove_stock
def remove_stock
puts "this is #{order_items}"
order_items.collect do |oi|
puts "this is product id: #{oi.product.id}"
end
end
However, on running the above the log is as follows:
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
Why is this showing so many product_ids, when there is only one product present in order_item?
PS: My associations are as follows:
product has_many order_items
order_items belongs_to order
order has_many order_items
What am I missing ?
Please help.
Thanks inadvance.
ruby-on-rails callback
add a comment |
up vote
0
down vote
favorite
I want to use after_update callback method for a method, which updates the product's stock in shopping cart application. The code in controller is:
ProductsController.rb:
def update
order = current_order
@order_item = order.order_items.find(params[:id])
@order_item.product.save
end
And in ProudctModel:
after_update :remove_stock
def remove_stock
puts "this is #{order_items}"
order_items.collect do |oi|
puts "this is product id: #{oi.product.id}"
end
end
However, on running the above the log is as follows:
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
Why is this showing so many product_ids, when there is only one product present in order_item?
PS: My associations are as follows:
product has_many order_items
order_items belongs_to order
order has_many order_items
What am I missing ?
Please help.
Thanks inadvance.
ruby-on-rails callback
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
because you're looping over all the order_items,order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to use after_update callback method for a method, which updates the product's stock in shopping cart application. The code in controller is:
ProductsController.rb:
def update
order = current_order
@order_item = order.order_items.find(params[:id])
@order_item.product.save
end
And in ProudctModel:
after_update :remove_stock
def remove_stock
puts "this is #{order_items}"
order_items.collect do |oi|
puts "this is product id: #{oi.product.id}"
end
end
However, on running the above the log is as follows:
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
Why is this showing so many product_ids, when there is only one product present in order_item?
PS: My associations are as follows:
product has_many order_items
order_items belongs_to order
order has_many order_items
What am I missing ?
Please help.
Thanks inadvance.
ruby-on-rails callback
I want to use after_update callback method for a method, which updates the product's stock in shopping cart application. The code in controller is:
ProductsController.rb:
def update
order = current_order
@order_item = order.order_items.find(params[:id])
@order_item.product.save
end
And in ProudctModel:
after_update :remove_stock
def remove_stock
puts "this is #{order_items}"
order_items.collect do |oi|
puts "this is product id: #{oi.product.id}"
end
end
However, on running the above the log is as follows:
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
order items product: 1
Why is this showing so many product_ids, when there is only one product present in order_item?
PS: My associations are as follows:
product has_many order_items
order_items belongs_to order
order has_many order_items
What am I missing ?
Please help.
Thanks inadvance.
ruby-on-rails callback
ruby-on-rails callback
edited Nov 20 at 12:24
asked Nov 20 at 12:11
Neha
305
305
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
because you're looping over all the order_items,order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32
add a comment |
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
because you're looping over all the order_items,order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
because you're looping over all the order_items,
order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
because you're looping over all the order_items,
order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
after_update :remove_stock
def remove_stock
puts "this is product id: #{self.id}"
end
since you're in Product model itself, you have access to the Product object
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I usedproduct.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.
– Neha
Nov 20 at 12:50
just writeself.order_items
ororder_items
directly it will work
– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392754%2fhow-to-use-after-update-callback-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
after_update :remove_stock
def remove_stock
puts "this is product id: #{self.id}"
end
since you're in Product model itself, you have access to the Product object
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I usedproduct.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.
– Neha
Nov 20 at 12:50
just writeself.order_items
ororder_items
directly it will work
– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
|
show 1 more comment
up vote
0
down vote
accepted
after_update :remove_stock
def remove_stock
puts "this is product id: #{self.id}"
end
since you're in Product model itself, you have access to the Product object
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I usedproduct.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.
– Neha
Nov 20 at 12:50
just writeself.order_items
ororder_items
directly it will work
– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
|
show 1 more comment
up vote
0
down vote
accepted
up vote
0
down vote
accepted
after_update :remove_stock
def remove_stock
puts "this is product id: #{self.id}"
end
since you're in Product model itself, you have access to the Product object
after_update :remove_stock
def remove_stock
puts "this is product id: #{self.id}"
end
since you're in Product model itself, you have access to the Product object
edited Nov 20 at 12:37
answered Nov 20 at 12:31
Ravi Mariya
934716
934716
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I usedproduct.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.
– Neha
Nov 20 at 12:50
just writeself.order_items
ororder_items
directly it will work
– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
|
show 1 more comment
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I usedproduct.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.
– Neha
Nov 20 at 12:50
just writeself.order_items
ororder_items
directly it will work
– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
This worked. One more doubt. I also what order_item object in the remove_stock method. How to get that?
– Neha
Nov 20 at 12:39
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
you can find the order_items for that product id in model
– Ravi Mariya
Nov 20 at 12:45
How? I used
product.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.– Neha
Nov 20 at 12:50
How? I used
product.order_items
, it threw error. I am sorry if I am missing something obvious, but I am new to rails and just learning these methods.– Neha
Nov 20 at 12:50
just write
self.order_items
or order_items
directly it will work– Ravi Mariya
Nov 20 at 12:50
just write
self.order_items
or order_items
directly it will work– Ravi Mariya
Nov 20 at 12:50
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
The table order_item contains a attribute quantity, which i want to use here in this method. So how to call that specific order_item associated with the product from the order_items collection?
– Neha
Nov 20 at 12:52
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392754%2fhow-to-use-after-update-callback-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you're printing order items id instead of product id, if I'm not wrong
– Ravi Mariya
Nov 20 at 12:19
Yes sorry about that. But my real question is, why is it looping through so many times? Even when I changed it to product.id, it is print the id as many times as shown in the question. Why so ?
– Neha
Nov 20 at 12:25
because you're looping over all the order_items,
order_items.collect do |oi|
– Ravi Mariya
Nov 20 at 12:27
I checked it. There is only one order_item in order_items.
– Neha
Nov 20 at 12:28
check the answer I have posted, can you try that code? and check
– Ravi Mariya
Nov 20 at 12:32