Query parent relation when querying for child records

Brit200313

Tech specs: ruby 2.1.5p273, Rails 4.2.3.

I need to pull a list of child records, based on it's own parameters and it's parent's parameters.

Child = Video

Parent = Player

The "date" attribute belongs to Video, the "org_id" belongs to Player.

I tried:

@videos = Video.where(date: start_date..end_date).includes(:player_page).
where(org_id: org_id)

but I get:

Mysql::Error: Unknown column 'videos.org_id' in 'where clause': SELECT `videos`...

basically, the second where clause is querying Videos, not Players.

To sum up: my intention is to query a set of Video records that fit within a date range and also match its parent's org_id. Thanks in advance for any help.

Sergei Stralenia

Try this:

@videos = Video.where(date: start_date..end_date).includes(:player_page).
where(player_page: { org_id: org_id })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting both parent and child items when querying child properties

From Dev

retrieve parent child records in one query

From Dev

Querying by parent and child key

From Dev

Rails query: get all parent records based on latest child records

From Dev

Lotus NotesSQL - "Joining" a child document to a parent when querying

From Dev

SQL Parent Child query - relation is defined in two tables

From Dev

Query to get parent records with child record, followed by next parent-child records in mysql

From Dev

Django was deleted parent relation when i delete child

From Dev

Django was deleted parent relation when i delete child

From Dev

Querying JavaScript Parent Child object

From Dev

Querying JavaScript Parent Child object

From Dev

SQL query using outer join and limiting child records for each parent

From Dev

SQL to find missing "child" records for each record in "parent" table of 1:many relation

From Dev

SQL to find missing "child" records for each record in "parent" table of 1:many relation

From Dev

Parent-Child relation in Qt

From Dev

Parent Child relation in SQL Server

From Dev

Parent Child relation in SQL Server

From Dev

SQL query to retrieve data which is in Parent and child relation in same table. Basically its a hierarchy table

From Dev

Query the last n,15 topics from a message board with a parent child relation

From Dev

SQL query for parent children relation

From Dev

Duplicating parent, child and grandchild records

From Dev

Delete Child Records with Missing Parent

From Dev

Symfony -- Sort child records in oneToMany relation

From Dev

Symfony -- Sort child records in oneToMany relation

From Dev

Selecting total of child records in their respective parent records

From Dev

Order Query by Parent and Child

From Dev

Select Parent and Child if child has records

From Dev

Convert java arrayList of Parent/child relation into tree?

From Dev

Parent and child relation setup from JSON

Related Related

  1. 1

    Selecting both parent and child items when querying child properties

  2. 2

    retrieve parent child records in one query

  3. 3

    Querying by parent and child key

  4. 4

    Rails query: get all parent records based on latest child records

  5. 5

    Lotus NotesSQL - "Joining" a child document to a parent when querying

  6. 6

    SQL Parent Child query - relation is defined in two tables

  7. 7

    Query to get parent records with child record, followed by next parent-child records in mysql

  8. 8

    Django was deleted parent relation when i delete child

  9. 9

    Django was deleted parent relation when i delete child

  10. 10

    Querying JavaScript Parent Child object

  11. 11

    Querying JavaScript Parent Child object

  12. 12

    SQL query using outer join and limiting child records for each parent

  13. 13

    SQL to find missing "child" records for each record in "parent" table of 1:many relation

  14. 14

    SQL to find missing "child" records for each record in "parent" table of 1:many relation

  15. 15

    Parent-Child relation in Qt

  16. 16

    Parent Child relation in SQL Server

  17. 17

    Parent Child relation in SQL Server

  18. 18

    SQL query to retrieve data which is in Parent and child relation in same table. Basically its a hierarchy table

  19. 19

    Query the last n,15 topics from a message board with a parent child relation

  20. 20

    SQL query for parent children relation

  21. 21

    Duplicating parent, child and grandchild records

  22. 22

    Delete Child Records with Missing Parent

  23. 23

    Symfony -- Sort child records in oneToMany relation

  24. 24

    Symfony -- Sort child records in oneToMany relation

  25. 25

    Selecting total of child records in their respective parent records

  26. 26

    Order Query by Parent and Child

  27. 27

    Select Parent and Child if child has records

  28. 28

    Convert java arrayList of Parent/child relation into tree?

  29. 29

    Parent and child relation setup from JSON

HotTag

Archive