On Sep 19, 2008, at 6:10 PM, Becca Girl wrote:

>
> Please see the above post for the setup of my models.
>
> While trying to figure this out, I put this into script/console:
>
>>> Book.find(1).reviews
>
> => [<#Review id: 2, reviewable_type: "Book", reviewable_id: 1,
> review_user_id: 1>]
>
>
> How can I take the value of review_user_id and get the name of the  
> user
> in the users table?
>
> Thanks!

You need to let ActiveRecord know about the association:
(You don't specify the Rails version you're using, but I'm guessing 2.x)

class Review < ActiveRecord::Base
  belongs_to :reviewable, :polymorphic => true
  belongs_to :review_user, :class_name => 'User'
  # or perhaps:
  # belongs_to :reviewer, :class_name => 'User', :foreign_key =>  
'review_user_id'
end

class Book < ActiveRecord::Base
  has_many :reviews, :as => reviewable
end

class User < ActiveRecord::Base
  has_many :reviews, :as => :reviewable  # can users be reviewed like  
Books, etc.?

  # if you mean for user.reviews to be the set of reviews
  # that this user wrote, then you want:
  has_many :reviews, :foreign_key => 'review_user_id'
end

Does that work for you?

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[EMAIL PROTECTED]



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to