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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:13:18+00:00 2026-05-27T17:13:18+00:00

In my model I have: attr_accessor :ppris def last(x) @ppris = [] if @ppris

  • 0

In my model I have:

attr_accessor :ppris

def last(x)
    @ppris = []

    if @ppris == nil
        @ppris << 1
    else
        @ppris << 2
    end
end

def pakke
  if self.pakkes.count > 0
  self.pakkes.each{|pakke| beregn_pakke(pakke.price)}
  end
end

In view a have a normal loop for my Company model.

<%= company.ppris %>

In view the value for ppris is [2] for all rows.

I have 5 Company rows in my table. I would expect that the value for ppris would be:
[1, 2, 2, 2, 2] for each company.

I have also changed the model method to:

def last(x)
@ppris = []
if @ppris == nil
@ppris << 1
else
@ppris << @ppris.last + 1
end
end

But then I get a NoMethodError. I would expect no error an ppris be:

[1, 2, 3, 4, 5]

What is wrong with my model method? Why can’t I create an array that gets added values for each pakke?

  • 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-05-27T17:13:19+00:00Added an answer on May 27, 2026 at 5:13 pm
    @ppris = []
    if @ppris == nil
    

    @ppris will never be nil. It’ll be empty?, but not nil.

    Addressing your comment

    You create a new array every time you call the method. If you want a single array associated with an instance, create one in the after_initialize callback.

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

Sidebar

Related Questions

In my model I have: validate :my_custom_validation def my_custom_validation errors.add_to_base(error message) if condition.exists? end
I have following code in my model def formatted_startdatetime unless startdatetime.nil? startdatetime.strftime('%d.%m.%Y %H:%M Uhr')
Let's say I have a simple model: class Subscription < ActiveRecord::Base attr_accessor :subscribed def
I have the following code in my model: attr_accessor :expiry_date validates_presence_of :expiry_date, :on =>
I have a model similar to the following: class Foo attr_accessor :attribute_a # Really
In rails 3.2.1, I have a model: class Player < ActiveRecord::Base attr_accessor :password attr_accessible
So I've followed the instructions here: http://doganberktas.com/2010/09/14/amazon-s3-and-paperclip-rails-3/ In my profile model I have: attr_accessor
In my ListItem rails model I have: attr_accessor :catalyst_action, :actor_user_id, :actor_full_name I'm able to
I have two models: Post and Tag. The Post model has an attr_accessor called
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor

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.