Forum: Ruby on Rails with_scope used in a has_many :through model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vincent AE Scott (Guest)
on 2006-04-03 18:17
(Received via mailing list)
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
This topic is locked and can not be replied to.