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

The Archive Base Latest Questions

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

I’m a completely new rails developer so bear with me :-) I have two

  • 0

I’m a completely new rails developer so bear with me 🙂

I have two models named Product and ProductPrice in rails.

This is how ProductPrices looks:

class CreateProductPrices < ActiveRecord::Migration
  def change
    create_table :product_prices do |t|
      t.integer :product_id
      t.date :since
      t.decimal :price
      t.boolean :current

      t.timestamps
    end
  end
end

And this is how the model looks:

class ProductPrice < ActiveRecord::Base
  belongs_to :product
  attr_accessible :current, :price, :product_id, :since
end

Now I want the price to be accesible through the Product. It should always show the current price (there can be only one current price/product. The user should also be able to edit the price through product. If the price has changed in an update of the Product then a new ProductPrice should be inserted with current = true and since = the current date. The old ProductPrice should no longer be current.

I already found articles about Nested Attributes and I think that would be the way to go. Can anyone tell me how I should go about to do this?

EDIT: Thanks for the reply, I tried implementing as you said, I have this now in product.rb:

class Product < ActiveRecord::Base
  belongs_to :menu_category
  belongs_to :menu
  has_many :product_prices, :dependent => :delete_all

  scope :latest_price, lambda {product_prices.where("since <= ?",DateTime.now).order(:since).last}
  attr_accessible :active, :descFR, :descNL, :image, :menu_category_id, :menu_id, :nameFR, :nameNL
  mount_uploader :image, ImageUploader    
end

And in my index page of products I have:

<tbody>
  <% @products.each do |product| %>
  <tr>
        <td><%= product.nameNL %></td>
        <td><%= product.nameFR %></td>
        <td><%= product.descNL %></td>
        <td><%= product.descFR %></td>
        <td><%= product.latest_price.price.to_s %></td>

I also tried: product.latest_price.price or product.latest_price
I also tried addint the latest_price to attr_accessible

But I always get this error:

undefined method `latest_price' for #<Product:0x49e1e48>

EDIT2: It now works. I also found out that If I add this:

  def latest_price=(val)
    pp = ProductPrice.where(:product_id => self.id, :since => DateTime.now.to_date).first_or_create(:price => val)
    pp.price = val
    pp.save
  end

Then it completely does what I asked for in the question.

  • 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-09T20:33:21+00:00Added an answer on June 9, 2026 at 8:33 pm

    I assume you have a Product model. Make sure you have the following in your model:

    class Product < ActiveRecord::Base
      has_many :product_prices
    
      def latest_price
        product_prices.where("since <= ?",DateTime.now).order(:since).last
      end
    end
    

    Now you can access all prices with:

    @product.find(123)
    @product.product_prices.each do |price|
      # each price for the product found here
    end
    

    But to access the latest price, the method gives you:

    @product.latest_price.price # latest price
    @product.latest_price.since # when the latest price was active since
    

    Notice I added the where("since <= ?",DateTime.now). That lets you schedule prices in advance (if since is in the future).

    To add a new price to the product:

    @new_price = ProductPrice.create(:price => ?,
                                     :product_id => @product.id,
                                     :since => DateTime.now)
    

    Another way to add a price to the product:

    @new_price = ProductPrice.create(:price => ?, :since => DateTime.now)
    @product.product_prices.push(@newprice)
    @product.save
    

    Or:

    @product.product_prices << @newprice # this autosaves
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.