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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:36:30+00:00 2026-06-05T08:36:30+00:00

Below is the code that I am using: This is my app/models file: class

  • 0

Below is the code that I am using:

This is my app/models file:

class One < ActiveRecord::Base
  attr_accessible :name, :surname      
end

class Two < ActiveRecord::Base  
  attr_accessible :name, :one_surname

  # generate a list of one's surnames
  def self.LIST_SURNAMES
    list  = Array.new
    arr   = One.all
    arr.each {|one| list << one.surname}

    return list
  end

  validates :one_surname, :inclusion => self.LIST_SURNAMES()
end

This is my /spec/models file:

FactoryGirl.define do
  factory :two do
    name "two_name"
  end

  factory :one do
    name "one_name"
    surname "one_surname"
  end    
end

describe Two do
  it 'should be created' do
    @one = FactoryGirl.create(:one)

    puts "One.last.surname = #{One.last.surname}"
    puts "Two.LIST_SURNAMES = #{Two.LIST_SURNAMES}"

    @two = FactoryGirl.build(:two, :one_surname => Two.LIST_SURNAMES[0])

    @two.save!
  end
end 

However, my test fails. And I’m entirely unsure of why this is. Any thoughts?

This is the RSpec output:

One.last.surname  = one_surname
Two.LIST_SURNAMES = ["one_surname"]

Further, I am getting this failure:

  1) Two should be created
     Failure/Error: @two.save!
     ActiveRecord::RecordInvalid:
       Validation failed: One surname is not included in the list
 # ./category_spec.rb:29:in `block (2 levels) in <top (required)>'
  • 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-05T08:36:32+00:00Added an answer on June 5, 2026 at 8:36 am

    It’s executing the Two.LIST_SURNAMES at interpretation time, not runtime. So if you want it to get the LIST_SURNAMES at runtime, you’ll need to use a proc

    validates :one_surname, :inclusion => {:in => proc {self.LIST_SURNAMES()}}
    

    The rest is optional, but here’s some cleaned code:

    class Two < ActiveRecord::Base
      attr_accessible :name, :one_surname
    
      validates :one_surname, :inclusion => {in: proc{One.pluck(:surname)}}                                              
    end
    

    I replaced the LIST_SURNAMES method with One.pluck(:surname) which does the same thing. Also: LIST_SURNAMES, if you keep it, should be def self.list_surnames since it is not a constant.

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

Sidebar

Related Questions

Below is the code that I'm using in ms access to list the data
I am using the below code, but I am not getting that why it
I have this javascript code below that uses jquery, it is suppoed to be
I'm using this code to download a web page in my Metro Style app:
I had the below code that load image from DB. There are more than
Below is my code that is not working and i dont understand why it
Below is come code that I am having trouble understanding. The output is 13
I have the code below that displays information about a computer. I am having
I have the code below that hides and shows the navigational bar. It is
I have the following code below that I'm trying to set a background color.

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.