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 8668521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:14:01+00:00 2026-06-12T18:14:01+00:00

Recently I have started to learn Rails. Now, I’m implementing the Friendship relation in

  • 0

Recently I have started to learn Rails.

Now, I’m implementing the Friendship relation in my own project. And here is the code some thing like that (some of them was collected from Internet)

class Friendship < ActiveRecord::Base 

  attr_accessible :friend_id, :message, :user_id, :approved

  validates_uniqueness_of :user_id, :scope => [:friend_id]

  validates :approved, :presence => true
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

end

/////////////////////

class User < ActiveRecord::Base
   has_many :friendships
   has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
   has_many :direct_friends, :through => :friendships, :conditions => { 'friendships.approved' => true }, :source => :friend
end

//////////////////////

class CreateFriendships < ActiveRecord::Migration
  def change
    create_table :friendships do |t|
    t.integer :user_id
    t.integer :friend_id
    t.text :message
    t.boolean :approved, :default => false

    t.timestamps
  end
end

When I create new Friendship from the console “rails c” it was OK when the approved = true

Friendship.create(:user_id=>7, :friend_id=>11, :approved=>1, :message=>'Be my friend :D')
(0.1ms)  begin transaction
SQL (0.9ms)  INSERT INTO "friendships" ("approved", "created_at", "friend_id", "message", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["approved", true], ["created_at", Sat, 13 Oct 2012 14:15:36 UTC +00:00], ["friend_id", 11], ["message", "Be my friend :D"], ["updated_at", Sat, 13 Oct 2012 14:15:36 UTC +00:00], ["user_id", 7]]
(12.2ms)  commit transaction

but when I set approved = false, it is down

Friendship.create(:user_id=>6, :friend_id=>7, :approved=>0)
(0.1ms)  begin transaction
(0.1ms)  rollback transaction
=> #<Friendship id: nil, user_id: 6, friend_id: 7, message: nil, approved: false, created_at: nil, updated_at: nil>

For the first time. I have thought it was the database(sqlite3) problem, but when I try this, from the sqlite3 terminal, it was fine

sqlite> insert into friendships (user_id, friend_id, created_at, updated_at) values(11, 6, '2012-10-13 14:15:36', '2012-10-13 14:15:36');
sqlite> select * from friendships;
        id = 1
   user_id = 11
 friend_id = 6
   message = 
  approved = f
created_at = 2012-10-13 14:15:36
updated_at = 2012-10-13 14:15:36

Can you tell me what happen and how to solve it.

  • 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-12T18:14:03+00:00Added an answer on June 12, 2026 at 6:14 pm

    You specify:

    validates :approved, :presence => true
    

    But in Rails:

    false.present? # => false
    

    Thus your validations are failing.

    You can verify this by looking at the validation errors:

    friendship = Friendship.create(:user_id=>6, :friend_id=>7, :approved=>0)
    friendship.errors
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have recently started to learn C# and have some questions regarding properties.
I have recently started a tutorial to learn how to code GUI using Windows
I have recently started to learn using of Doctrine 2.0 framework for PHP. Now
I have recently started trying to learn MPI to code in C. I am
I have recently started to learn Objective-C and write my tests using OCUnit that
I have a friend and he recently started to learn Cinema 4D. He asked
Recently I have started learning rails and was a little surprised that the default
I have recently started using RSpec for Integration testing in my Rails application, to
I have recently started using DocCheck for checking the validity of JavaDoc's in code
Ignore my novice knowledge in jQuery. I started to learn recently and I have

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.