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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:47:19+00:00 2026-06-09T12:47:19+00:00

I have a model called Treating that requires the presence of both a ‘requestor’

  • 0

I have a model called Treating that requires the presence of both a ‘requestor’ and a ‘requestee’. Neither of these attributes are accessible in the Treating model. Here’s the model:

class Treating < ActiveRecord::Base
  attr_accessible :intro, :proposed_date, :proposed_location

  validates :intro, presence: true, length: { maximum: 190 }
  validates :requestor_id, presence: true
  validates :requestee_id, presence: true

    belongs_to :requestor, class_name: "User"
    belongs_to :requestee, class_name: "User"

    default_scope order: 'treatings.created_at DESC'

end

I am trying to populate the test database with this rake file, sample_data.rake:

namespace :db do
    desc "Fill database with sample data"
    task populate: :environment do
        ...

        requestors = User.all(limit: 6)
        50.times do
            intro = Faker::Lorem.sentence(5)
            requestors.each do |requestor|
                requestee = User.find(rand(1..6))
                requestor.sent_treatings.create!(intro: intro)
            end
        end
    end
end

Unless I make :requestor an accessible attribute in the Treating model and follow ‘intro: intro’ with ‘requestee: requestee’ in the ‘requestor.sent_treatings.create!(intro: intro)’ line of my rake file, I will get a Validation error when I run rake db:populate saying that ‘requestee is blank’:

rake aborted!
Validation failed: Requestee can't be blank

Tasks: TOP => db:populate

1) [more imporant] How do I set ‘requestee’ in my rake file without having to make it accessible in the model?
2) I have a hacky approach to declaring the ‘requestee’ variable within my ’50.times.do’ loop: ‘requestee = User.find(rand(1..6))’. What is a better way to declare this similar to how I declare my ‘requestor’ variable in ‘requestors.each do |requestor|’?

Thanks so much!

  • 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-09T12:47:20+00:00Added an answer on June 9, 2026 at 12:47 pm
    namespace :db do
        desc "Fill database with sample data"
        task populate: :environment do
            ...
    
            requestors = User.all(limit: 6)
            requestees = User.find((1..6).to_a)
            50.times do
                intro = Faker::Lorem.sentence(5)
                requestors.each do |requestor|
                    requestee = requestees.sample #random array member, Ruby 1.9+
                    treating = requestor.sent_treatings.build(intro: intro)
                    treating.requestee = requestee
                    treating.save!
                end
            end
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called company that has_many users then users belongs_to company. class
I have a model called user and another model called student. Neither of them
I have a model called Story that I'm trying to order by the created_at
I have a model called websites that has_many :likes , and of course another
I have a model called HeroStatus with the following attributes: id user_id recordable_type hero_type
I have a model called a voip_phone that has a an attribute schedule which
I have a model called Genre There are already many records stored in that.
I have a model Rails model called Orders that has a type_id, location, and
I have a Model called Section which has many articles ( Article ). These
I have a model called ClassFile that takes a FileField (document): class ClassFile(models.Model): timestamp

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.