开发者

Rails 3: change the balance through associated objects

I have a small Rails app, that has as model a "Shop" and another model named "Purchases".

开发者_Go百科

Shop has a balance (as decimal) and Purchases has an amount (as decimal). Now, I can create purchases that are affiliated with a shop, however, the only thing missing is that I want is to be able to manipulate the balance once I create a new purchase.

So let's say the balance is initial 0.0 and I create a new purchase that belongs to that shop, how can I add (or substract) the amount from the purchase from the balance and store that in the Shop?

Thanks,


I would make a method that allowed you to "purchase" something from the shop. You can either pass in a Purchase object to this or if the information is simple enough, have the Shop object do this internally. Then I'd make Store add the Purchase to its internal list of purchases and update its balance attribute.

Alternatively if performance wasn't an issue then you could just make the balance method return the total of all the associated Purchases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜