views:

79

answers:

3

I have 3 models

User
has_many :quetions
has_many :corrections
end

Question
has_one :correction
belongs_to :user
end

Correction
belongs_to :user
belongs_to :question

So if user Bob asks a question then user Terry can check it and if its wrong offer a correction.

Lets stay with bob and assume he as kindly corrected 5 other users, i.e and lets assume he has been lucky to get 3 corrections from other users.

I want to be able to do something like this

@bob.corrections_offered => 5 correction objects @bob.corrections_received => 3 correction objects

the first one is easy as its really just @bob.corrections under the hood. But i dont know how to implement the latter one. Can anyone help?

UPDATE

SO i tried using through as suggested like so (Oh and actually the question model above is actually called Sentence in my code. I.e. User => Sentence => Correction. )

has_many :sentences
has_many :corrections_received, :through => :sentences, :class_name => 'Correction'

but got this error in console

ctiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :Correction in model Sentence. Try 'has_many :corrections_received, :through => :sentences, :source => '. Is it one of :language, :correction, :user, or :checker?

So tried the following

has_many :corrections_received, :through => :sentences, :source => :correction 

but got

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one for has_many :corrections_received, :through => :sentences. Use :source to specify the source reflection.

not sure whats going wrong...

+1  A: 

Normally, you should be able to do it with :through, but I'm not sure if two user->correction relationships are possible.

In any case, helper method in the model class should be simple enough. Something like this.

  def corrections_received
    result = Array.new
    questions.each do |q|
      if q.correction
        result.push q.correction
      end
    end
    result
  end

Since I'm new to this stuff, corrections are welcome!

Nikita Rybak
You should always avoid doing something like this. Because a method like this can significantly effect performance if you have large dataset or as your data grows. You must do a sql join query with conditions for stuff like this instead of getting all data and then discarding them based on some conditions. But in any case, in rails for something like this you can use `has many through` relationships
nas
thanks for the reply. I thought of this but as per nas reply i was worried about performance.
adam
+2  A: 

You can add a has_many through relationship in your user model like so

class User
  #your usual relationships
  has_many :corrections_received, :through => :questions, :class_name => 'Correction'
end
nas
hi nas im getting some errors trying to implement that. Can you check my question above for the update
adam
hi nas, thank you for the reply. Im getting some errors though on this (see my update). can you figure out whats causing this
adam
+1  A: 

try this way: has_many :corrections_received,:class_name=>'Correction',:conditions=>...

psjscs
thank you for your reply.
adam