Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:18:19+00:00 2026-06-04T09:18:19+00:00

I’ve set up projects, tasks, and subtasks, all of wich are commentable. I have

  • 0

I’ve set up projects, tasks, and subtasks, all of wich are commentable. I have created comments model and connected the models but don’t know how to properly validate for user id and how to test model code using Rpsec and Factory Girl. I am also using Devise.

Comments Migration

  1 class CreateComments < ActiveRecord::Migration
  2   def change               
  3     create_table :comments do |t|   
  4       t.references :user, :null => false
  5       t.text :comment,  :null => false,
  6                         :limit => 500                   
  7       t.references :commentable, :polymorphic => true
  8         # upper line is a shortcut for this
  9         # t.integer :commentable_id     
 10         # t.string :commentable_type    
 11       t.timestamps         
 12     end                    
 13     add_index :comments, :user_id   
 14     add_index :comments, :commentable_id
 15   end
 16 end

Comment Model

1 class Comment < ActiveRecord::Base
  2         
  3   # RELATIONSHIPS   
  4   belongs_to :commentable, :polymorphic => true
  5   belongs_to :user
  6   # VALIDATIONS            
  7   validates :user, :presence => true
      validates :commentable, :presence => true
  8   validates :comment, :presence => true,
  9                       :length => { :maximum => 500 }  
 10 
 11   # ATTRIBUTE ASSIGNMENT   
 12   attr_accessible :comment 
 13   
 14 end

User Model

  1 class User < ActiveRecord::Base
  2         
  3   # RELATIONSHIPS   
  4   has_many :synapses, :dependent => :destroy
  5   has_many :projects, :through => :synapses
  6 
  7   has_many :vesicles, :dependent => :destroy
  8   has_many :tasks, :through => :vesicles
  9 
 10   has_many :subvesicles, :dependent => :destroy
 11   has_many :subtasks, :through => :subvesicles, :dependent => :nullify
 12   
 13   has_many :comments, :dependent => :destroy
 14   
 15   # VALIDATIONS
 16   validates :name,  :presence => true,
 17                     :uniqueness => true             
 18 
 19   # ATTRIBUTE ASSIGNMENT   
 20   attr_accessible :name, :email, :password, :password_confirmation, :remember_me
 21 
 22   # DEVISE MODULES         
 23   # Include default devise modules. Others available are:
 24   # :token_authenticatable, :encryptable, :lockable, :timeoutable and :omniauthable
 25   devise :database_authenticatable, :registerable,
 26          :recoverable, :rememberable, :trackable, :validatable,
 27          :confirmable      
 28 
 29 end

Comment Factory

105   factory :comment do
106     comment "This is some generic comment with some generic content"
107   end

Comment Model Specs

  1 require 'spec_helper'      
  2         
  3 describe Comment do
  4 
  5   it 'Should create new comment' do
  6     FactoryGirl.build(:comment).should be_valid
  7   end                      
  8 
  9   it 'Should respond to method provided by polymorphism to find its parent' do
 10     FactoryGirl.build(:comment).should respond_to(:commentable)
 11   end                      
 12 
 13 end 

This 1st test currently fails with error message saying undefined method `user’ for #Comment:0xa88c354>. But if I pass in user id like this…

FactoryGirl.build(:comment, :user => confirmed_user).should be_valid

Than I should have user id set up as mass assignable attribute, and I don’t want that (figure that some users might mess with that attribute and change it). How to test and validate this properly? Also, this is my 1st time doing polymorphic, so if you see anything silly, let me know.

Edit. I have now done this as one answer suggested. Unfortunately, it returns the same error.

  5   it 'Should create new comment' do
  6     confirmed_user = FactoryGirl.build(:confirmed_user)
  7     FactoryGirl.build(:comment, :commentable => confirmed_user).should be_valid 
  8   end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:18:20+00:00Added an answer on June 4, 2026 at 9:18 am

    Try to replace:

    :user => confirmed_user
    

    with:

    :commentable => confirmed_user
    

    UPD:

    validates :user, :presence => true
    

    also has to be:

    validates :commentable, :presence => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.