Tester123
Tester123

Reputation: 229

Rails database finding data by id

SO this is probably going to get super confusing.

Basically i have a web application, The "Event" has an email where users can send out an email of their selected options.

So far the options save down to the database. the user id is saved in the table event_bookings, The id for this is linked into the package_bookings as event_id, This has a package ID which links to the package where i need the details from.

the TLDR is this

Table > Table >Table > Table that has the data i need

@event_booking.package_bookings.each

This currently shows everything inside the table with the packages id so basically i have access to the package id (in byebug) however i need to go and get the next level and show the package details

Any help would be lovely, I may have worded this super wrong!

Heres the little bit of byebug code that works

    (byebug) @event_booking.package_bookings.first.id
  PackageBooking Load (0.3ms)  SELECT  "package_bookings".* FROM "package_bookings" WHERE "package_bookings"."event_booking_id" = $1  ORDER BY "package_bookings"."id" ASC LIMIT 1  [["event_booking_id", 6]]
23
(byebug) @event_booking.package_bookings.first
  CACHE (0.0ms)  SELECT  "package_bookings".* FROM "package_bookings" WHERE "package_bookings"."event_booking_id" = $1  ORDER BY "package_bookings"."id" ASC LIMIT 1  [["event_booking_id", 6]]
#<PackageBooking id: 23, package_id: 23, event_booking_id: 6, quantity_tickets: 1, created_at: "2015-11-19 08:55:06", updated_at: "2015-11-19 08:55:06", sub_options_selected: [27]>

Upvotes: 0

Views: 42

Answers (1)

Nitin
Nitin

Reputation: 7366

As per your explanation you can find package detail like this:

@event_booking.package_bookings.each do |package_booking|
  @package = package_booking.package ## You can find package detail like this
end

You can also add has_many :through Association in this case. Detail here.

Upvotes: 1

Related Questions