Been developing in Rails for about a month and I'm still working on
grasping its subtleties.
Can someone please shed some light on this simple scenario?
## Schema ##
create_table :books do |t|
t.string :title
t.integer :year
end
create_table :pages do |t|
t.integer :no
t.references :book
end
Which will both allow you to refer to the books fields in the where
clause, and return an collection of object graphs rather than page
object corrupted with fields they shouldn't have.
Which will both allow you to refer to the books fields in the where
clause, and return an collection of object graphs rather than page
object corrupted with fields they shouldn't have.
SELECT
"pages"."id" AS t0_r0,
"pages"."no" AS t0_r1,
"pages"."book_id" AS t0_r2,
"pages"."created_at" AS t0_r3,
"pages"."updated_at" AS t0_r4,
"books"."id" AS t1_r0,
"books"."title" AS t1_r1,
"books"."year" AS t1_r2,
"books"."library_id" AS t1_r3,
"books"."created_at" AS t1_r4,
"books"."updated_at" AS t1_r5
FROM
"pages"
LEFT OUTER JOIN "books" ON "books".id = "pages".book_id
WHERE
(books.library_id = 1)
The real "books" and "pages" have decimal and large text columns that
are not always needed.
Could be worth considering extracting those fields into another table
and have a 1:1 link... a little messy, but saves you returning lots of
data when you only need a small part of it.