eager-loading


Rails eager loading and conditions


I have the following associations set up
class bookinghdr
belongs_to :agent
end
class bookingitem
belongs_to :bookinghdr, :include => agent
end
So I was expecting to be able to do the following:
named_scope :prepay, :include=>["bookinghdr"], :conditions => ["bookinghdr.agent.agenttype = 'PP'"]
and in my controller do:
b = Bookingitem.prepay
But that gives me a ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'bookinghdr.agent.agenttype'
However if I don't include the conditions clause then I get a recordset on which I can do:
b = Bookingitem.prepay
b[0].bookinghdr.agent.agenttype
without any error!!
I don't want to have to get all the records and then iterate over them to find the ones whose agent has a 'PP# flag. I was hoping that AR would do that for me.
Anybody got any ideas on how to achieve this?
Thanks in advance.
Purvez
Your question shows that you have not yet fully understood how associations and named scopes work. Since I cannot tell from your question what parts aren't clear, I suggest you read the Association Basics guide at http://guides.rubyonrails.org/v2.3.11/association_basics.html. This should bring you up to speed regarding the concepts you want to implement. After you have read the guide it should all make sense.

Related Links

How to enable eager loading on a association in phpactiverecord?
Rails eager loading with conditions
Rails eager loading and conditions
Eager loading for globalize2 translations

Categories

HOME
webview
extjs
azure-functions
keyboard
tomcat
enums
loopbackjs
celery
elisp
apache-kafka-connect
bittorrent
joomla3.2
kurento
responsive-design
cocotb
spring-security-oauth2
heuristics
max-msp-jitter
vmware-workstation
jaspersoft-studio
cloudsim
xsl-fo
http-authentication
el
chronicle-queue
react-bootstrap-table
fatfs
reporting
lxml
gsp
xmlunit
cloveretl
directfb
des
jade4j
twilio-php
android-alarms
llvm-ir
gitolite
system-on-chip
grails3.2.0
raytracing
robust
type-inference
common-table-expression
realstudio
section508
windows-media-player
winston
winmerge
htmlspecialchars
lvalue
java-2d
okio
dpkg
android-expansion-files
diawi
juniper-network-connect
asp.net-web-api-helppages
screen-orientation
gitlab-omnibus
wlanapi
postal-code
flow-js
erlog
innerhtml
camus
comctl32
flurry-analytics
wicket-1.5
dmake
lumia-imaging-sdk
siena
sql-function
windows-taskbar
sensormanager
colon
rikulo
aero
story
first-class
image-scanner
monocross
wcf-callbacks
fbml
solandra
cassini
efs
code-camp
community-server
jvm-bytecode
account-management

Resources

Encrypt Message