[Rails] Re: How to specify condition for eagerly joined model using
by Jw other posts by this author
Jun 19 2007 3:04PM messages near this date
[Rails] Re: Is "target" a reserved word?
|
[Rails] Rails + Rest
Hi Mark,
Thank you _so_ much for your code and your return trip to update/fix it
above as well. We have a large schema with tons of lookup tables
defining entity type/kind and the problem as original descibed has come
up a bunch.
Your code above DOES work as described. I just set this up and was able
to do:
Person.with_conditions(:addresses,
'addresses_mailing.id_t_addresses_mailing = 2') do
@people = Person.find :all, :include => [{:addresses =>
:t_address}, :email_addresses], :conditions => "name_last like 'W%'",
:limit => 15
end
which, includes multiple other associations, a limit clause and some
WHERE clause action as well.
All best!
> > class User
> > has_many :conditional_addresses, :className => Address do
> > def conditions=(condition)
> > reflection.options[:conditions] = condition
> > end
> > end
> > end
> >
> > User.conditional_addresses.conditions = '...'
> > User.find :all, :include => :conditional_addresses
>
> Just for the record, this won't work because conditional_addresses
> is a User instance method, not a class method, so with this code
> you'd have to write: User.new.conditional_addresses.conditions = '...'
>
> Better would be:
>
> class User < ActiveRecord::Base
> has_many :addresses
>
> def self.with_conditions(assoc, conditions)
> options = reflect_on_association(assoc).options
> orig_conditions = options[:conditions]
> options[:conditions] = conditions
> yield
> options[:conditions] = orig_conditions
> end
> end
>
> User.with_conditions(:addresses, 'addresses.is_active = 1') do
> User.find :all, :include => :addresses
> end
>
> You could even augment method_missing and add a class context
> so you can instead write:
>
> User.with_address_conditions('addresses.is_active = 1') do
> find :all, :include => :addresses
> end
>
> --
> We develop, watch us RoR, in numbers too big to ignore.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Ta
lk" group.
To post to this group, send email to rubyonrails-talk@[...].com
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@[...].com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
|