0

In my rails appI have four models( say A, B, C, D, E and F)

  • A belongs to B
  • B has many Cs
  • C belongs to D
  • D belongs to E and F

I am trying to build query as follows

scope = A.joins(:b, { b: [:cs, {cs: [:d, {d: [:e,:f] } ] } ] }) 

But it is not working. The error message is schema cs (plural of c) does not exist.

I am using Postgresql.

2
  • dont forget: joins(:c) then joins(:d) Commented Dec 2, 2014 at 9:00
  • could you show the resulted query? Commented Dec 2, 2014 at 9:26

1 Answer 1

1

Well, it seems like you're missing a B reference (b_id) in the C model. C needs to know to which B it belongs. After that, you can simplify the query to this:

A.joins(b: {cs: {d: [:e, :f]}}) 
Sign up to request clarification or add additional context in comments.

4 Comments

I have the reference(without it the app wouldn't work properly). But thanks for the shorter form, though I still have the same error message
The error is on the where clause. scope.where("lower(c.d.description) like ?", "%#{params[:description]}%")
Try scope.where("lower(ds.description) like ?", "%#{params[:description]}%")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.