views:

54

answers:

1

Well the situation is bit more complicated than that !

I have a question model

class Question < ActiveRecord::Base
  has_many :answers, :dependent => :destroy

then an answer Model

class Answer < ActiveRecord::Base
  belongs_to :question, :counter_cache => true
  has_many :rep_events, :class_name => "RepEvent", :foreign_key => "event_id", :dependent => :destroy

and Finaly a rep_event model

class RepEvent < ActiveRecord::Base
  belongs_to :answer   
end

My rep event doesnt have any primary key. it only has event_id that acts like answer_id

When i destroy a Question, I want to delete everything that is related to it ( Question, Answers and Rep_Events ) Thats why I'm using :dependent => :destroy I tried to enter the console and test it but it gives me some error

NoMethodError: undefined method `eq' for nil:NilClass

This error is very general but I believe the problem is that my rep_event class does not have any "answer_id" field but only event_id. thats why I used foreign_key => "event_id" in my relationship.

Can anyone tell me what the problem is ?

Thank you

A: 

The foreign key is not present in the Answer model, instead RepEvent model has a foreign key called 'event_id' that acts like answer_id, and links to the Answer model. If this scenario is right you will have to do following changes in the RepEvent model

class RepEvent < ActiveRecord::Base
  belongs_to :answer, :foreign_key => event_id
end

and in your Answer model

class Answer < ActiveRecord::Base
  belongs_to :question, :counter_cache => true
  has_many :rep_events, :class_name => "RepEvent", :dependent => :destroy
end

Try this this must work.

Rohit
Almost there! that give me an error that answer_id is not found.I cant rename my table as its not only for answers but for other events. I will have to find out how to make it search for rep_events.event_id and not rep_events.answer_id.That is the error:ActiveRecord::StatementInvalid: PGError: ERROR: column rep_events.answer_id does not exist
can you show what exactly your migration for rep_events consist of I think there is a reference missing in your table from rep_events to answer
Rohit
Sorry its bit messy as a comment: create_table :rep_events, :id => false do |t| t.integer "event_id", :null => false t.string "event_type", :limit => 12, :null => false t.integer "user_id", :null => false t.integer "author_id", :null => false t.datetime "created_at", :null => false
are you using a polymorphic association called events. If this is the case you might have to change the code in your model
Rohit
@user387221 you need to add a reference from the rep_events table to the answers table like we do in normal scenarios in which rails connects the id's somewhat like this t.references :answers this line will create a column answer_id as a foreign key to answers. but in your case this line would be somewhat different. I am trying to figure it out. :D
Rohit
my model doesnt refer to answers only but many events. hence I call the foreign key : event_id. is there any way to keep it as it is ? thanks a lot
The event_id column is a part of polymorphic association already. I don't think there is a way to use the same column in two different associations as a reference. :D . Sorry but I think you will have to add another column as answer_id. Hope you find a better solution.
Rohit