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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:57:10+00:00 2026-06-16T01:57:10+00:00

I am trying to learn Rspec in a very simple CRUD Rails 3.2.8 app.

  • 0

I am trying to learn Rspec in a very simple CRUD Rails 3.2.8 app. I’m following the general pattern of Michael Hartl’s examples and have been moderately successful with cucumber for the outside in portion. Now I want to test a Twilio SMS feature and cannot seem to get to first base, mostly because I’m not asking the right questions, so I expect to be corrected here and get back on track.
My app has two models, commodity and price and they interact with each other in my cucumber tests, so it appears. I’m aware, like in cucumber, I need an object to start to test its interactions. In my prices controller, I see that I can get the commodity’s prices with the below in my prices#create method:

 @price = @commodity.prices.build(params[:price])

I ultimately want to generate a factory that will have many prices for a given commodity. But I want to get to base first. Following thoughtbot’s examples on their Readme I’m attempting the following in rails console:

 FactoryGirl.create(:commodity) do |price|
   Commodity.prices.build(attributes_for(:price))
 end

The result is: NoMethodError: undefined method `prices’ for #
Hmm, I must not be understanding either Rspec or Factory Girl. Here is my basic factories.rb:

 FactoryGirl.define do
   factory :commodity do
     name "corn"
   end

   sequence :price do |n|
     price
     date { Time.now }
   end
 end

Here are my two models:

 class Commodity < ActiveRecord::Base
   attr_accessible :description, :name
   has_many :prices
 end

 MOST_RECENT = 5
 class Price < ActiveRecord::Base
   attr_accessible :buyer, :date, :price, :quality, :commodity_id
   scope :most_recent, lambda { order("id desc").limit(MOST_RECENT) }
    belongs_to :commodity
 end

My attempt to understand this is to do it simply in Rails console but the error also appears when I run rspec as well. But why would FactoryGirl, or Rspec, not seem to use the prices method I get with Active Record? Clearly, I’m not understanding something or I would have found the answer on Stack, thanx, sam

  • 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-16T01:57:12+00:00Added an answer on June 16, 2026 at 1:57 am

    In your FactoryGirl.create there are a couple problems. First, the block argument should be commodity, not price. create passes the created object into the block. Second, you’re trying to run prices on the Commodity class. In your object relationship, prices is an accessor associated with a specific instance. There is no Commodity#prices method, but any given instance of Commodity will have prices. You can probably use build like that, but I think the canonical way is to use the shift operator to add a Price.

    Putting this together gets you:

    FactoryGirl.create(:commodity) do |commodity|
      commodity.prices << FactoryGirl.create(:price, commodity: commodity)
    end
    

    I’m not sure what you’re doing with the sequence in your Commodity factory definition. If you’re trying to make sure that Commodities are created with Prices by default (without adding them as above), check out some of the tips at http://icelab.com.au/articles/factorygirl-and-has-many-associations/.

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

Sidebar

Related Questions

I'm trying to learn RSpec and writing test for CRUD actions. Here is my
I am trying to learn rails from http://ruby.railstutorial.org/ Exercise 3 explains to install rspec,
Trying to learn asio, and I'm following the examples from the website. Why is
I'm following through the Learn Rails by Example book, and I'm trying to run
Trying to learn about php's arrays today. I have a set of arrays like
Trying to learn MVP pattern with C#... Does anyone know of any particularly good
Im trying to learn a bit about c++ and have run in to some
I was trying learn lex and yacc using the oreilly book. I tried following
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've

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.