With_scope used in a has_many :through model


#1

Hi there,
I have something like the following:

class Person < AR:B


has_many :votes
has_many :chosen_answers, :through => :votes, :source => :answer,
:select => ‘DISTINCT answers.*’ do
def my_answers(whodunnit)
chosen_answers.with_scope(:find => { :conditions => [
“votes.created_by=?”, whodunnit ] }) do
chosen_answers.find :all
end
end
end

Its somewhat of a contrived example, but I’m trying to understand if/how
it can work.

If i try:
p=Person.find :first

p.chosen_answers.my_answers( Person.find_by_name(‘me’).id )

The above errors with:
NoMethodError: undefined method `chosen_answers’ for Answer:Class

OK, so i tried Person.chosen_answers.with_scope and get the following:
NoMethodError: undefined method `chosen_answers’ for Person:Class

But, if i try the following

p=Person.find :first
p.chosen_answers.with_scope( :find =>
{ :conditions => [ “votes.created_by=?”, p.id ] }) do
p.chosen_answers.find :all
end

It behaves correctly and returns some results that are scoped.

I’m figuring that what I’m trying to do here is either not possible, or
not the correct RoR way of achieving what I’m trying todo.

Any pointers?


keys: http://codex.net/gpg.asc

Any dictator would admire the uniformity and obedience of the U.S.
media.
– Noam Chomsky