Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:02:11+00:00 2026-05-24T17:02:11+00:00

I want to know if you know how to write the following sql query

  • 0

I want to know if you know how to write the following sql query in a
more “rails way”

UPDATE  buy_order_detail
SET saldo = saldo - detail_purchase.cantidad
FROM purchase_detail
           INNER JOIN purchase ON purchase.id =
purchase_detail.purchase_id
           INNER JOIN waybill ON waybill.id =
purchase.waybill_id
           INNER JOIN buy_order ON buy_order.id =
waybill.order_id
           INNER JOIN buy_order_detail ON buy_order.id =
buy_order_detail.order_id
WHERE purchase_detail.product_id = buy_order_detail.product_id 

I’ve tried something like this, in the “DetailPurchase model”

def after_create
    pchs = Purchase.find(self.purchase_id)
    wbl = Waybill.find(pchs.waybill_id)
    bdr = BuyOrder.find(wbl.buy_order_id)
   BuyOrderDetail.find_by_buy_order_id(bdr.id).where(:product_id => self.product_id).update_attribute(:saldo, buy_order_detail.saldo - cantidad)
  end

But when I check the value “saldo”, it always keep the same original value

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:02:12+00:00Added an answer on May 24, 2026 at 5:02 pm

    Writing the question using the Rails Way, would implore that you

    1. write Models
    2. use Associations between the models to define their relations
    3. call upon Model.find() with some conditions to generate the SQL (automatically and out-of-sight)

    All these steps (and more) are explained in the Getting Started with Rails documentation

    With Ruby on Rails one (normally) does not generate SQL directly. But data from a database is modeled by (duh) Models. These models are used by Rails to retrieve the requested information from the database. Basically, you could say that each Model corresponds with a table in the database (at least for simple situations).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a SQL-Server query but I don't know how to solve.
I want to write the following query: SELECT ..., MIN(SomeBitField), ... FROM ... WHERE
I want to write a query for MS SQL Server that adds a column
I want to know how to correctly write an if statement with jquery. I
I want to write a real-time analysis tool for wireless traffic. Does anyone know
I want to write to a file in the windows temp directory. I know
OK, I know what you're thinking, "why write a method you do not want
I am learning about Unit Testing and want to know how to write testable
I want to write a following script: given a text file with the list
I want to write a query to see if a category field is within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.