Karena di class Kamar sudah ada belongs_to :hotel, cukup melakukan ini di
controller:

def view
  @kamar = Kamar.find(params[:id])
  # untuk mendapatkan object hotel yang "memiliki" si kamar
  @hotel = @kamar.hotel
end

cmiiw

On 10/23/07, secreet admirer <[EMAIL PROTECTED]> wrote:
>
>   Saya bingung, mohon bantuan. Saya mo tampilkan data yang ada di tabel
> lain yang terelasi antar tabel. Berikut Keterangannya :
>
> ----------------------------------------------------------
> Location : app/models/kamar.rb
> ----------------------------------------------------------
> class Kamar < ActiveRecord::Base
> belongs_to :hotel
>
> ...
>
> def self.find_all_in_kamar(hotel)
> hotel = Hotel.find_by_name(hotel)
> self.find :all, :conditions => "id_hotel = #{Hotel.id}"
> end
>
> ...
>
> ----------------------------------------------------------
> Location : db/migrate/kamar.rb
> ----------------------------------------------------------
> class CreateKamars < ActiveRecord::Migration
> def self.up
> create_table :kamars do |t|
> t.column :id_hotel, :string, :limit => 8
> ...
> end
>
> execute 'ALTER TABLE kamars ADD CONSTRAINT fk_kamars_hotels
> FOREIGN KEY ( id_hotel ) REFERENCES hotels( id ) ON DELETE CASCADE'
> ...
>
> end
>
> ----------------------------------------------------------
>
> Gimana caranya supaya supaya di view/kamar/show.rhtml saya bisa
> menampilkan informasi semua data hotel yang memiliki id hotel sesuai dengan
> id_hotel pada tabel kamar. Apa yang harus saya tulis di :
>
> class KamarController < ApplicationController
>
> ....
> def show
> @kamar = Kamar.find(params[:id])
> ?? Apa yang harus saya tulis disini utk menampilkan data hotel??
> end
> ....
>
> Saya sudah coba ini dan berikut pesan pesan error mereka (T_T)
>
> -----------------------------------
> Experimen pertama
> ----------------------------------
> def show
> @kamar = Kamar.find(params[:id])
> @hotel = Hotel.find(:all, :conditions => "id = '[EMAIL 
> PROTECTED]<%27%23%7B%40Kamar.id>_hotel}'"
> )
> end
>
> --------------------------------------------
> Experimen pertama && Error
> --------------------------------------------
>
> NoMethodError in KamarController#show
>
> You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.id_hotel
>
>
> Hiks.. banyak experimennya sampe takut kepanjangan... PLEASE ID-RUBY PIMP
> MY CODING. sekalian kalo boleh sama coding buat show.rhtml nya.
> [-o< Pleaseeee...
>
> ---------------------------------
> Bergabunglah dengan orang-orang yang berwawasan, di bidang Anda di Yahoo!
> Answers
>
> [Non-text portions of this message have been removed]
>
>  
>


[Non-text portions of this message have been removed]

Kirim email ke