Ich bin ein Anfänger, wenn es um Schienen geht, so dass jede Hilfe sehr geschätzt wird. Hier ist ein Teil meiner Abfrage von meinem Controller ist:Wie wähle ich ein Attribut in einem Array in Rails?
def index
@bookings = Booking.where(["end_at >= :endDate", { endDate: Date.today.to_s}])
@ownerID = @bookings.collect{|booking| Player.select("user_id").where("booking_id = :bookingID", {bookingID: booking})}
end
und hier ist ein Teil meiner Ansicht, die ich ausgeben bin versucht:
<tbody>
<% @bookings.each_with_index do |booking, index| %>
<tr>
<td><%= booking.id %></td>
<td><%= booking.court_id %></td>
<td><%= @ownerID.values_at(index) %></td>
<td><%= booking.start_at %> to <%= booking.end_at %></td>
<td><%= booking.paid? ? 'Paid' : 'Not Paid' %></td>
<td><%= link_to 'Cancel', edit_admin_court_path(booking) %></td>
</tr>
<% end %>
<tbody>
Ich sammle die Werte richtig von meiner db, aber ich habe Probleme, nur den Wert für die user_id selbst zu drucken. Zur Zeit, dass Spalte ausgibt dies:
[#<ActiveRecord::Relation [#<Player id: nil, user_id: 4>]>]
aber ich will nur die 4 von den user_id
ich die letzten paar Stunden damit verbracht, auf Arrays und AR zu lesen, aber ich weiß nicht, genug, um wirklich noch zu Sinn machen. Danke im Voraus.
EDIT: Hier ist mein Modell für die Reservierung: Klasse Buchung < Activerecord :: Base # belongs_to: Benutzer belongs_to: Gericht has_many: Kommentare
has_many :players
has_many :users, through: :players
attr_accessor :day, :start_time, :end_time, :type_of_court
validates :start_at, :day, :start_time, :end_time, presence: true
validate :is_available?
validate :at_least_one_hour?
before_validation(on: :create) do
self.key ||= SecureRandom.hex(5)
if !day.blank?
stime = Time.parse(start_time)
self.start_at = Time.strptime(day, "%m-%d-%Y")+stime.hour.hours+stime.min.minutes
etime = Time.parse(end_time)
self.end_at = Time.strptime(day, "%m-%d-%Y")+etime.hour.hours+etime.min.minutes
end
self.half_court = type_of_court == 'Half Court'
true
end
def at_least_one_hour?
if !end_at.nil? && !start_at.nil?
if (end_at - start_at) < 1.hour
self.errors[:base] << "The booking is only available for periods of 1 hour or more"
end
end
end
def is_available?
bookings = Booking.where('start_at BETWEEN (?) AND (?) or end_at BETWEEN (?) AND (?)', start_at, end_at, start_at, end_at)
is_free = if bookings.count == 0
true
else
bookings.first.end_at == start_at || bookings.first.start_at == end_at
end
# check if is available in court schedules
if is_free
booking_day_type = start_at.saturday? ? 'Saturday' : start_at.sunday? ? 'Sunday' : 'Weekday'
schedule = court.schedules.where(day_type: booking_day_type).first
if schedule.nil?
is_free = false
else
start_at_hours, start_at_minutes = schedule.start_at.split(':')
end_at_hours, end_at_minutes = schedule.end_at.split(':')
schedule_start_at = Time.new start_at.year, start_at.month, start_at.day, start_at_hours, start_at_minutes, 0
schedule_end_at = Time.new end_at.year, end_at.month, end_at.day, end_at_hours, end_at_minutes, 0
is_free = (schedule_start_at..schedule_end_at).cover?(start_at) && (schedule_start_at..schedule_end_at).cover?(end_at)
end
end
if !is_free
self.errors[:base] << "The court isn't available on these hours"
end
return is_free
end
def owner
owner = players.includes(:user).where(owner: true).first
owner.user if owner
end
def by_admin?
owner.nil? || owner.is_admin?
end
def court_type
self.half_court ? 'Half Court' : 'Full Court'
end
def full_date
"#{self.start_at.strftime('%d %b')}, #{self.time_date}"
end
def time_date
"#{self.start_at.strftime('%I:%M')}-#{self.end_at.strftime('%I:%M')} #{self.start_at.strftime('%p')}"
end
def only_day_date
"#{self.start_at.strftime('%b. %d')}"
end
def book_price
price.nil? ? self.court.price : price
end
end
hier das Modell für die Spieler ist:
class Player < ActiveRecord::Base
belongs_to :booking
belongs_to :user
end
und hier ist ein Teil meines Schema:
create_table "bookings", force: true do |t|
t.integer "court_id"
t.datetime "start_at"
t.datetime "end_at"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "half_court", default: false
t.float "price"
t.boolean "paid"
t.string "key"
end
add_index "bookings", ["court_id"], name: "index_bookings_on_court_id", using: :btree
add_index "bookings", ["key"], name: "index_bookings_on_key", unique: true, using: :btree
create_table "players", force: true do |t|
t.integer "booking_id"
t.integer "user_id"
t.boolean "owner", default: false
end
add_index "players", ["booking_id"], name: "index_players_on_booking_id", using: :btree
add_index "players", ["user_id"], name: "index_players_on_user_id", using: :btree
Klingt wie Sie Assoziationen verwenden müssen (http://guides.rubyonrails.org/association_basics.html). Wenn Sie Ihre Modelle für Booking/Player und auch Ihr Schema veröffentlichen, kann ich vielleicht mehr Hilfe anbieten. – Austio
Ich würde denken, dass es das Sammeln ist, das Dinge durcheinander bringt, nein? Ich kann die gewünschten Werte abrufen, ich kann sie einfach nicht aus ActiveRecord herausziehen. –
Bitte fügen Sie Ihre schema.rb-Datei hinzu –