zgadza sie - czyli majac kategorie:
2.1.2 :023 > Category.all.as_json
Category Load (0.5ms) SELECT "categories".* FROM "categories"
=> [{"id"=>1, "name"=>"Category 1", "created_at"=>Tue, 03 Jun 2014 06:09:55 UTC +00:00, "updated_at"=>Tue, 03 Jun 2014 06:09:55 UTC +00:00}, {"id"=>2, "name"=>"Category 2", "created_at"=>Tue, 03 Jun 2014 06:09:57 UTC +00:00, "updated_at"=>Tue, 03 Jun 2014 06:09:57 UTC +00:00}]
i usera ze startem w kategorii nr 1 ( i evencie nr 1):
User.first.starts.as_json
User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
Start Load (0.2ms) SELECT "starts".* FROM "starts" WHERE "starts"."user_id" = ? [["user_id", 1]]
=> [{"id"=>1, "name"=>"Start #1", "user_id"=>1, "event_id"=>1, "category_id"=>2, "created_at"=>Tue, 03 Jun 2014 06:09:37 UTC +00:00, "updated_at"=>Tue, 03 Jun 2014 06:10:12 UTC +00:00}]
Starty user w kategorii nr 1 (nie ma):
2.1.2 :030 > User.first.starts.where(category: Category.find(1)).as_json
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
Start Load (0.2ms) SELECT "starts".* FROM "starts" WHERE "starts"."user_id" = ? AND "starts"."category_id" = 1 [["user_id", 1]]
=> []
i kategoria nr 2:
2.1.2 :031 > User.first.starts.where(category: Category.find(2)).as_json
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 2]]
Start Load (0.3ms) SELECT "starts".* FROM "starts" WHERE "starts"."user_id" = ? AND "starts"."category_id" = 2 [["user_id", 1]]
=> [{"id"=>1, "name"=>"Start #1", "user_id"=>1, "event_id"=>1, "category_id"=>2, "created_at"=>Tue, 03 Jun 2014 06:09:37 UTC +00:00, "updated_at"=>Tue, 03 Jun 2014 06:10:12 UTC +00:00}]
pozdrawiam
Marek