My application is on Rails 2.
I have 2 models, joined by a has_and_belongs_to_many
relation : Message
and Conversation
.
So I have 3 tables, messages
, conversations
and conversations_messages
.
Is it possible to load in one unique query both objects Message
and Conversation
, without using the :include
option ? The :include
option is impossible because my query depends of some external params.
Example :
I can do a query from Message to have a couple of (Message
,Conversation
) per line
messages = Message.find(:all,
:select => "messages.*, conversations.*",
:conditions => some_conditions_depending_of_external_variables
:group => "messages.id"
)
The resulting table looks like :
`messages`.id | `messages`.field | `conversations`.id | `conversations`.field
--------------+------------------+--------------------+------------------
1 | ... | 10 | ...
--------------+------------------+--------------------+------------------
2 | ... | 15 | ...
--------------+------------------+--------------------+------------------
3 | ... | 10 | ...
--------------+------------------+--------------------+------------------
4 | ... | 20 | ...
After this query, my variable messages
contains a set of AR Messages elements, but the conversations fields are not loaded as an AR Conversation.
I want to be able to call a message.linked_conversation to have the conversation of the same line, as an AR object.
Is there any method to do this without re-loading the objects ?