1

I have two models - Customer and Contractors. I have setup a simple app, where they interact on an activity. Now at the end of it, I would like for them to leave each other feedbacks. Nothing complex just a database field of comment.

I am wondering what is the right model association to have here?

I tried this

class Customer 
  has_many :feedbacks
end

class Contractor
  has_many :feedbacks
end

class Feedback
  belongs_to :customer
  belongs_to :contractor
end

But the problem here is identifying who commented who.

For instance, if I do

customer = Customer.find(1)
contractor = Contractor.find(1)
customer.feedbacks.create(:comment => "Contractor 1 sucks", :contractor_id => 1)

The problem is, its accessible by both contractor.feedbacks and customer.feedbacks. And I dont know who commented who now.

Any guidance is appreciated. Am i missing something?

Thanks

6
  • Are companies or contractors the same thing? you seem to switch half way down
    – Kyle Macey
    May 25, 2013 at 18:21
  • Yes, sorry I mixed up two things. I have updated my question.
    – psharma
    May 25, 2013 at 18:22
  • Are you wanting some feedbacks to apply to both? You have, for example: customer.feedbacks.create(:comment => "Contractor 1 sucks", :contractor_id => 1) which is assigning the "Contractor 1 sucks" feedback to both the customer and to the contractor. Is that what you mean? BTW, I wouldn't do it that way anyway. I'd do something like feedback = customer.feedbacks.create(:comment => "Contractor 1 sucks"), and then assign it to the contractor with contractor.feedbacks << feedback.
    – lurker
    May 25, 2013 at 18:26
  • no there arent any inside the system. I am trying to build one from scratch. Where customers can leave feedback for contractors and vice versa.
    – psharma
    May 25, 2013 at 18:28
  • I don't think you understand my question. My question is: do you want to allow a single feedback that applies to both a customer and contractor?
    – lurker
    May 25, 2013 at 18:29

1 Answer 1

3

The way to do this would be to use polymorphic associations.

This way, you could have a commenter relationship, and a commentable relationship.

Like so:

class Customer 
  has_many :feedbacks, as: commenter
  has_many :feedbacks, as: commentable
end

class Contractor
  has_many :feedbacks, as: commenter
  has_many :feedbacks, as: commentable
end

class Feedback
  belongs_to :commenter, polymorphic: true
  belongs_to :commentable, polymorphic: true
end

Now, Feedback will require four new columns:

  • commentable_type:string
  • commentable_id:integer
  • commenter_type:string
  • commenter_id:integer

All four should be indexed, so write your migrations appropriately. The type columns will store a String value of the model name associated ("Customer" or "Contractor").

So you can do things like:

  @feedback = Feedback.find 3
  @feedback.commenter
    => # Some Customer

  @feedback.commentable
    => # Some Contractor

And vise versa. You would build like:

@customer = Customer.find 1
@contractor = Contractor.find 1
@feedback = Feedback.new comment: "This is a great Contractor"
@feedback.commenter = @customer  # You can reverse this for a contractor giving feedback to a customer
@feedback.commentable = @contractor
@feedback.save!
3
  • Hey Kyle, unfortunately the customer_id and contractor_id gets set to nil. Any clue why? The other two fields get save fine.
    – psharma
    May 25, 2013 at 20:16
  • When done this inside the view <%[email protected]%> returns NilClass
    – psharma
    May 25, 2013 at 21:00
  • I can help you more maybe Monday. I'm not going to be home until then. Edit your question with your code and your migrations. Watch the video at the first link in my answer, too. That should help answer a lot of issues.
    – Kyle Macey
    May 26, 2013 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.