Mouse.The.Lucky.Dog
Mouse.The.Lucky.Dog

Reputation: 755

Rails conditions on joined table

I have tables a,b. Associated classes

class A < ActiveRecord::Base
   has_one :b
end

class B <ActiveRecord::Base
end

b has integer field c.

I want to retrieve all records of A that have a B which has c< 5.

I've tried

A.find(:conditions => (B.c < 5) )

but I get complaints "undefined member c".

What is the best way to accomplish this?

Upvotes: 1

Views: 467

Answers (3)

SSP
SSP

Reputation: 2670

Don't use find. Go ahead with "where"

A.joins(:b).where("b.c < 5")

A.b.where("c < 5")

Upvotes: 1

xdazz
xdazz

Reputation: 160863

Try this:

A.joins(:b).where("b.c < 5")

Upvotes: 1

Kyle C
Kyle C

Reputation: 4097

Try this

 A.b.where("c < 5")

dont use the find contions, it is decremented with rails 3

Upvotes: 0

Related Questions