Polymorphism messing with me

Maybe one of you guys can figure this out:

User

  • has_many :comments

Comment

  • belongs_to :user
  • belongs_to :commentable, :polymorphic => true

Story

  • has_many :comments, :as => commentable

I have a story (variable called story) and I say:
story.comments.create :user_id => current_user.id

and now story.comments contains this comment but not user.comments.
The comment that was created has a commentable_id, a commentable_type,
but not a user_id, even if I specify it explicitly like above.

Am I going about creating the comment wrong or did I setup the
association wrong?

Thanks,
JB

no your setup is right and so is the creating
but in that case you may have to reload the User object after saving the
new Comment. ActiveRecord objects even if very smart in some ways,
mainly reflect the state of the db at the point of their creation (eg
User.find)
Rails updates Story immediately, but does not check for all other
possibly involved models or checks if there are any objects of them in
memory at this time.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs