D
D
Denis2018-06-21 14:06:59
Ruby on Rails
Denis, 2018-06-21 14:06:59

There are two models room and hotel, how to bind many rooms to one hotel?

class Hotel < ApplicationRecord
  belongs_to :user
  has_many :pictures, :as => :imageable

  validates :hotel_type, presence:true
  validates :num_room, presence:true
  validates :food, presence:true

  geocoded_by :address
  after_validation :geocode, if: :address_changed?

  def cover_photo(size)
    if self.pictures.length > 0
      self.pictures[0].image.url(size)
    else
      "blank.jpg"
    end
  end
end

class Room < ApplicationRecord
  enum instant: {Запрос: 0, Мгновенное: 1}

  belongs_to :user
  has_many :pictures, :as => :imageable
  has_many :reservations

  has_many :guest_reviews
  has_many :calendars

  geocoded_by :address
  after_validation :geocode, if: :address_changed?

  validates :home_type, presence: true
  validates :room_type, presence: true
  validates :accommodate, presence: true
  validates :bed_room, presence: true
  validates :bath_room, presence: true


  def cover_photo(size)
    if self.pictures.length > 0
      self.pictures[0].image.url(size)
    else
      "blank.jpg"
    end
  end

  def average_rating
    guest_reviews.count == 0 ? 0 : guest_reviews.average(:star).round(2).to_i
  end
end

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question