0

シナリオは、u ユーザー has_many head_bookings と head_bookings has_many の予約があり、アパートメントが予約に属しているというものです。head_booking には注文番号があります。したがって、ユーザーがログインすると、head_booking (注文番号) によってアパートメント グループの予約が取得されます。

は私のモデルです

    class User < ActiveRecord::Base
      has_many :head_bookings
    end



class HeadBooking < ActiveRecord::Base

    has_many :bookings  
    belongs_to :user
  # attr_accessible :title, :body
end

class Booking < ActiveRecord::Base
  # attr_accessible :title, :body
  belongs_to :appartment
  belongs_to :head_booking
  accepts_nested_attributes_for :head_booking

end

テーブルにダミーデータを作成し、コンソールでこれを試しました:

u = User.find(1)
u.head_bookings
u.head_bookings.bookings

コマンド u.head_bookings.bookings を使用すると、「undefined method `bookings'」というエラーが表示されます

私は何を間違っていますか?? ありがとう..レムコ

4

3 に答える 3

1

予約をユーザーに関連付けることができます。

class User < ActiveRecord::Base
  has_many :head_bookings
  has_many :bookings, through: :head_bookings
end

次に、次の方法でユーザーの予約を選択できます。

u = User.find(1)
u.bookings
于 2013-09-09T13:54:33.077 に答える
1

ユーザーの予約を直接操作する予定がある場合は、UserBookingusingの間に関係を追加する必要がありhas_many throughます。

class User < ActiveRecord::Base
  has_many :head_bookings
  has_many :bookings, through: :head_bookings
end

u.bookingsこれにより、ヘッド予約を通じて参加しているユーザーのすべての予約を取得するようなことができます。

于 2013-09-09T13:54:49.310 に答える
0

その後、User has_many :head_bookingsa を実行するu.head_bookingsとリレーションが返され(配列として機能する)、配列内で a を実行することはできません.bookings

次のようなものを試してください

u.head_bookings.each do |hb|
  b.bookings
end

bookingsそれぞれの を使用しますhead_booking

于 2013-09-09T13:48:14.250 に答える