My first problem is that im not quiet sure if this could work at all. But since i’ve already got a lot of help here i’ll at least try to ask something myself.
What i have here is a model ‘thesis’, which has_many ‘tasks’ through a relation model and also has_many ‘checked_tasks’. Now i want to have only those theses where the number of tasks euqals the number of checked_tasks.
class Thesis < ActiveRecord::Base
has_many :tasks, :through => :relations
has_many :checkedtasks
end
class Task < ActiveRecord::Base
belongs_to :relation
end
class CheckedTask < ActiveRecord::Base
belongs_to :thesis
end
My first idea was simply to to a Thesis.where(self.tasks.count == self.checked_tasks.count) – but i coudn’t figure out what to use instead of the self to get it working.
After that, i tried to do it whit scopes, but that still leaves me with the same problem.
After all, i’m not sure if there is a ‘rails way’ to do that – if so, i’d be very thankful for some help
You could do
But this is quite a pig of a query to run. it really feels like you should be redesigning your data model along the lines suggested by some of the commenters, using the join model between thesis and task to store the checkness of a task for a thesis.