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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:08+00:00 2026-05-25T15:15:08+00:00

I’m using Rails 3.1 and have a simple transaction class that references a User

  • 0

I’m using Rails 3.1 and have a simple transaction class that references a User model:

class User < ActiveRecord::Base
   has_many :transactions, :foreign_key => 'sender_id'
end

class Transaction < ActiveRecord::Base
   belongs_to :sender, :class_name => 'User'
   belongs_to :recipient, :class_name => 'User'
   attr_accessible :amount
end

The database schema is

create_table "transactions", :force => true do |t|
   t.decimal "amount"
   t.integer "sender_id", :null => false
   t.integer "recipient_id", :null => false
end

create_table "users", :force => true do |t|
end

I would like to create a set of initial transactions in the seeds.rb, but the underlying INSERT is never generated with foreign keys for the sender_id and recipient_id. The signature of the method is:

ruby-1.9.2-p290 :022 >   Transaction.method(:create) => 
   #<Method: Transaction(id: integer, 
                         amount: decimal,
                         sender_id: integer,
                         recipient_id: integer)
   (ActiveRecord::Base).create> 

I’ve tried both

Transaction.create(
  amount:                0.50,
  sender:                User.first,
  recipient:             User.last,
)

and

Transaction.create(
  amount:                0.50,
  sender_id:             User.first.id,
  recipient_id:          User.last.id,
)

In each case, the INSERT statement is

SQL (0.6ms)  INSERT INTO `transactions` (`amount`, `recipient_id`, `sender_id`) 
             VALUES (0.75, NULL, NULL)

I’m new to rails, so I’m sure this is a misunderstanding on my part, but I have not been able to find a solution from my reading of the rails docs.

  • 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-05-25T15:15:09+00:00Added an answer on May 25, 2026 at 3:15 pm

    Simple solution. I just needed to change the attr_accessible line in the Transaction model to

    attr_accessible :amount, :sender, :recipient
    

    and all is well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.