Changing activerecord associations at runtime

Is there any way to change the associations of a model at runtime,
like before running a specific query?

Chris

snacktime wrote:

Is there any way to change the associations of a model at runtime,
like before running a specific query?

Chris

Do you mean changing what tables certain models are linked to? If so,
this is a very bad idea, but you might be able to do some clever ruby
hacking to make it work. Definitely not easy.

On 11/28/06, Alex W. [email protected] wrote:

I want to drop associations for certain queries. Say I have a
Customer model that has many transactions and subscriptions.
Sometimes I just want to return the customer record only. The catch
is that this is for a webservice with a static api defined in a wsdl,
so I can’t for example create a CustomerWithoutTransactions model and
just use that in place of a Customer record.

For example here is the struct that is returned to the client:

CustomerRec < ActionWebService::Struct
member :transactions, [TransactionRec] # transactions also have
associations.
member :subscriptions, [Subscription]
member :customer, Customer
end

I’ve tried replacing the customer member with a model that is just
like Customer but without any associations. Didnt’ work and threw
errors about the field that is the primary key not being defined.
Probably because it goes to resolve the associations somehow at
runtime and finds they don’t exist.

What would really be nice is an :exclude option to find that runs the
query without any joins on associated tables. So in find with with
:exclude => :transactions, customers.transactions would just be an
empty array.

Chris