I guess I have to chalk to up to another "I didn't know you could do
that". I'll also admit I have no idea how efficient this is.
I've used a technique where I query a belongs_to table with the ids of
a has_many related table. In my test I have
Assessments has_many -> Questions has_many -> Answers
I'd do something like
answer_ids = Answer.where(:requires_other =>
true).select(:question_id).map(&:question_id)
# @foo.map(&:id) is shorthand for @foo.map{|i| i.id}, which gets
array of ids
question_ids = Question.where(:id =>
answer_ids).select(:assessment_id).map(&:assessment_id)
@assessments = Assessment.where(:id => question_ids)
Or, if you want one big hard to read statement!
@assessments = Assessment.where(:id => Question.where(:id =>
Answer.where(:requires_other =>
true).select(:question_id).map(&:question_id)).select(:assessment_id).map(&:assessment_id))
Someone else will have to chime in on how efficient or inefficient
this is. My last project was on a database that was very reliant on
"sets" and this is the closest thing I've found in rails to that
concept.
Steve
On Oct 15, 7:00 am, Max Reznichenko <[email protected]> wrote:
> Hi, everyone.
>
> Yesterday found an interesting issue in scope method.
> Let`s say, you have next model structure:
>
> Model A --has_many-> Model B --has_many-> Model C --has_many-> Model_D
>
> Model D has some attribute, let`s say, 'status' ENUM('opened',
> 'closed').
>
> In Model A I would like to take all records, where Model D`s status is
> opened.
> This is a little bit confusing.
>
> Here is an example.
> We have a project. A project has many tasklists. Tasklist has many
> tasks.
> I would like take all projects, which have one or more tasks opened.
> I hope, there should be any possibility to call in controller just
> active_projects = Project.all.active.
>
> I would appreciate any help.
>
> Max Reznichenko
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.