@Mimuss

Почему возвращается 3 nil'a в запросе?

Использую PostgreSQL
2.5.0 :003 > Book.joins("INNER JOIN grants ON grants.book_id = books.id").where('grants.date' => Date.new(2000,1,1)..Date.today).select("grants.date as grants_date")
  Book Load (1.9ms)  SELECT  grants.date as grants_date FROM "books" INNER JOIN grants ON grants.book_id = books.id WHERE ("grants"."date" BETWEEN $1 AND $2) LIMIT $3  [["date", "2000-01-01"], ["date", "2018-03-14"], ["LIMIT", 11]]
 => #<ActiveRecord::Relation [#<Book id: nil>, #<Book id: nil>, #<Book id: nil>]>

Сами данные выглядят так: Book
2.5.0 :013 > Book
 => Book(id: integer, name: string, cipher: string, created_at: datetime, updated_at: datetime, library_id: integer, author: string, publisher: string, published_date: date, price: float, release_date: date)

Grant:
2.5.0 :014 > Grant
 => Grant(id: integer, date: datetime, book_id: integer, subscriber_id: integer, created_at: datetime, updated_at: datetime, date_of_return: date)

Таблица Books
spoiler
Table "public.books"
     Column     |            Type             |                     Modifiers                      
----------------+-----------------------------+----------------------------------------------------
 id             | bigint                      | not null default nextval('books_id_seq'::regclass)
 name           | character varying(2000)     | not null
 cipher         | character varying(100)      | not null
 created_at     | timestamp without time zone | not null
 updated_at     | timestamp without time zone | not null
 library_id     | bigint                      | 
 author         | character varying(50)       | 
 publisher      | character varying(50)       | 
 published_date | date                        | 
 price          | double precision            | 
 release_date   | date                        | 
Indexes:
    "books_pkey" PRIMARY KEY, btree (id)
    "index_books_on_library_id" btree (library_id)
Foreign-key constraints:
    "fk_rails_9bb3dacd9b" FOREIGN KEY (library_id) REFERENCES libraries(id)
Referenced by:
    TABLE "grants" CONSTRAINT "fk_rails_76e4d1cfb5" FOREIGN KEY (book_id) REFERENCES books(id)

Таблица Grants
spoiler
Table "public.grants"
     Column     |            Type             |                      Modifiers                      
----------------+-----------------------------+-----------------------------------------------------
 id             | bigint                      | not null default nextval('grants_id_seq'::regclass)
 date           | timestamp without time zone | not null
 book_id        | bigint                      | 
 subscriber_id  | bigint                      | 
 created_at     | timestamp without time zone | not null
 updated_at     | timestamp without time zone | not null
 date_of_return | date                        | 
Indexes:
    "grants_pkey" PRIMARY KEY, btree (id)
    "index_grants_on_book_id" btree (book_id)
    "index_grants_on_subscriber_id" btree (subscriber_id)
Foreign-key constraints:
    "fk_rails_75dda46977" FOREIGN KEY (subscriber_id) REFERENCES subscribers(id)
    "fk_rails_76e4d1cfb5" FOREIGN KEY (book_id) REFERENCES books(id)
  • Вопрос задан
  • 38 просмотров
Пригласить эксперта
Ответы на вопрос 1
Dem1
@Dem1 Куратор тега Ruby on Rails
Ruby on Rails developer
Потому что Вы в select не включили id
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы