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

  • Home
  • SEARCH
  • 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 184429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:17:26+00:00 2026-05-11T15:17:26+00:00

I have a User model and a Friendship-Model. class Friendship < ActiveRecord::Base belongs_to :sender,

  • 0

I have a User model and a Friendship-Model.

class Friendship < ActiveRecord::Base   belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id'   belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id'    validates_presence_of :receiver_id, :sender_id   validates_associated :receiver, :sender end  class User < ActiveRecord::Base   has_many :sent_friendships, :class_name => 'Friendship', :foreign_key => 'sender_id', :dependent => :destroy   has_many :received_friendships, :class_name => 'Friendship', :foreign_key => 'receiver_id', :dependent => :destroy end 

and one of my rspec-test is

describe Friendship do    before(:each) do     @valid_attributes = {       :sender_id => 1,       :receiver_id => 2,       :accepted_at => nil     }   end    it 'should not be valid with non-existant receiver_id' do     f = Friendship.new(@valid_attributes)     f.receiver_id = 99999     f.should_not be_valid   end end 

The test shouldnt be valid, because there is no User with user_id 9999. But the test say’s the friendship-model is valid.

Why the hell?

EDIT:

But I want to test like mentioned -> without assigning the sender directly. Isn’t that possible??

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T15:17:27+00:00Added an answer on May 11, 2026 at 3:17 pm

    If you want your model to work that way, change this:

    validates_presence_of :receiver_id, :sender_id 

    to this:

    validates_presence_of :receiver, :sender 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my User model: class User < ActiveRecord::Base has_many :friends, :class_name => 'Friendship', :dependent
I have User model and Group model. User belongs_to Group . Group has an
I have a Friendship model which looks like this: from_user = ForeignKey(User, related_name='friends') to_user
I have the following - simplified - models: class User(models.Model): following = models.ManyToManyField(self, through='Following',
I have a User model that I am using devise for. Each User belongs_to
Possible Duplicate: CakePHP Subscribe Users Model I have three models User Post Friendship What
I have a message model and a user model. my message belongs_to my user
Let's say I have User model and Post model. Post model contains field user_id
I have a User model that has many posts. Im trying to render the
I have a user model on which I check to make sure that the

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.