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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:16:19+00:00 2026-06-07T22:16:19+00:00

I have adopted and I’m not sure exactly why something isn’t working. I have

  • 0

I have adopted and I’m not sure exactly why something isn’t working.

I have a pricable polymorphic association which I use for only a single model called Item. It looks like this:

class Item < ActiveRecord::Base
  #price
  has_one :price, :as => :pricable
  accepts_nested_attributes_for :price

  attr_accessible :price_attributes, :price, ....

I’d like to add to an Event model and have added the following:

class Event < ActiveRecord::Base
  #price
  has_one :price, :as => :pricable
  accepts_nested_attributes_for :price
  attr_accessible :price, :price_attributes

However, I am not able to set it:

ruby-1.9.2-p290 :001 > e=Event.find(19) #ok
ruby-1.9.2-p290 :002 > e.price
Creating scope :page. Overwriting existing method Price.page.
  Price Load (0.8ms)  SELECT `prices`.* FROM `prices` WHERE `prices`.`pricable_id` = 19 AND `prices`.`pricable_type` = 'Event' LIMIT 1
 => nil 
ruby-1.9.2-p290 :003 > e.price.price=23
NoMethodError: undefined method `price=' for nil:NilClass
    from /Users/jt/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from (irb):3

Hmmm…. it looks like the relationship is set up correctly and that Event has access to price via attr_accessible. Any idea what else could be going on?

thx

  • 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-07T22:16:21+00:00Added an answer on June 7, 2026 at 10:16 pm

    Relationship appears to be defined correctly, however if e.price returns nil then obviously e.price.price= won’t work and return undefined method error. You need to build/create an associated price object first:

    > e = Event.find(19)
    => #<Event id: 19, ...>
    > e.price
    => nil
    > e.create_price(price: 23)
    => #<Price id: 1, priceable_id: 19, price: 23, ...>
    

    or if you would like to use nested attributes:

    > e = Event.find(19)
    => #<Event id: 19, ...>
    > e.price
    => nil
    > e.update_attributes(price_attributes: { price: 23 })
    => true
    > e.price
    => #<Price id: 1, priceable_id: 19, price: 23, ...>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In many application I often have algorithms which make use of dedicated sub-algorithms (or
I am not sure if I have asked the question correctly. Please correct me
Following suggestions on this site, I have adopted SimpleXML from org.simpleframework.xml. I use this
We, as I'm sure many, have adopted the approach whereby interfaces are pre-pended with
I have adopted and adapted an HTML5 canvas extension which draws dotted lines, but
I have a few general use functions that do not really make sense in
I have adopted an iOS app and am having problem asynchronous requests. We have
I have a simple java project (adapted from the example here ), which is
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=

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.