0

I want to run next code in Rails:

boatData = Boat.find_by_sql(["SELECT bm.name as name1, bt.name as name2 
                                  FROM boat_models bm, boat_types bt, boats b
                                  WHERE b.id = ?
                                  AND bm.id = b.boat_model_id
                                  AND bt.id = bm.boat_type_id", boatId]).first
    puts "boatData=" + boatData.inspect
    description = "Booking: " + boatData[:name1] + "(" + boatData[:name2].to_s + ")"

The only problem is the fields in the select have the same name. When I try to add "as" the results are not right.

UPDATE:

This it the output of puts "boatData=" + boatData.inspect

boatData=#<BoatModel id: nil, name: "Velero">
2
  • what do you mean by "not right"? Commented Mar 11, 2014 at 13:14
  • See my updated comment Commented Mar 11, 2014 at 13:30

1 Answer 1

1

I could be very wrong, but it looks like you just want to select a Boat and then get the model and type name for that boat.

If I understand your data model correctly, you can use:

boat = Boat.find(boatId)
modelName = boat.boat_model.name
typeName = boat.boat_model.boat_type.name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.