Re: [Rails] Getting child collection with find_by_sql
On 20 June 2011 20:58, Herman <hermanccw@gmail.com> wrote:
> Hi all,
>
> I have a simple Parent "has_many" Child model relationships, and I
> have the following query.
>
> Parent.find_by.sql(["select p.* from parent p inner join p.id on
> c.parent_id where <some calculation> and c.enabled = :cond", {:cond =>
> true}])
>
> This got all the parent object out and it was good. However, when I
> do Parent.children collection, it gives me back all the chile
> regardless if the child is enabled or disabled (the condition check
> for child in the query).
Your find_by_sql just gets the Parent objects - the Child association
objects are populated by a separate sql request when you call
parent.children (you can confirm this by looking in the logs).
> Is there way to do this?
Set up a named_scope in Parent for :enabled_children.
BTW Is there really some reason why you have to use find_by_sql, and
you can't just use a normal finder?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home