Undefined method 'unshift' - ruby-on-rails

I'm creating a new Rails app with Rails 6 and I want to do something like this:
#items = Item.all.unshift Item.new(id: 0, name: '-- Kein Item --')
But I get the error "undefined method `unshift' for #<ActiveRecord::Relation [#<Item id: 1, name: "Item 1", created_at: "2021-03-13 11:09:34.284978000 +0000", updated_at: "2021-03-13 11:09:34.284978000 +0000">]>"
In another Rails app I can do it, but that app is running under Rails 4.
I want to add this "empty" record so that model project hast not everytime an item and because I have somewhere Project.item.name it won't give an nil error.
Any suggestions?

Related

Trouble accessing column value rails console

I'm trying to access the integer stored in a column called totalVotes in my UserVotes table but keep getting a No Method Error
I have been trying to debug from the console:
1.9.3-p327 :006 > uservote = UserVote.where("soundcloud_id = 68061927")
UserVote Load (0.2ms) SELECT "user_votes".* FROM "user_votes" WHERE (soundcloud_id = 68061927)
=> [#<UserVote id: 5, user_id: "1", party_profile_id: "1", soundcloud_id: "68061927", totalVotes: 0, created_at: "2013-02-19 04:57:58", updated_at: "2013-02-19 04:57:58">]
Good so far...
1.9.3-p327 :007 > uservote.totalVotes
NoMethodError: undefined method `totalVotes' for #<ActiveRecord::Relation:0x007febb49ab350>
Then it breaks and I'm not sure why. What is the best way for me to access the totalVotes value?
your issue is you're calling .totalVotes to an AR Relation. call .first first then you should have no problem.
>> uservote = UserVote.where("soundcloud_id = 68061927").first
>> uservote.totalVotes

Rails 3.0.9 Model attributes are lost on database query

If i open the console and type in the following code, my model looses attributes. In all cases only the first attribute after the id is accessable. I had this problem on one server. My old server is working fine with the same code and same versions. Here the output:
irb(main):001:0> User.new
=> #<User id: nil, encrypted_uid: nil, encrypted_access_token: nil, created_at: nil, updated_at: nil>
irb(main):002:0> User.first
=> #<User id: 1, encrypted_uid: "I7lPHOYoGMNWki3cZtb5oA==\n">
ActiveModel::MissingAttributeError (missing attribute: encrypted_access_token):
Has anyone an idea to get it working? Thanks in advance.
I had to recreate the application and copy the model into hte new application. after that everything worked fine. No clue what was wrong.

Access just the value of BSON::ObjectId in ruby

I'm working on a ruby/rails app backed by Mongodb (using Mongoid). Within the context of the Rails application everything works flawlessly but we're also accessing objects outside of the Rails environment, where I'm having trouble getting the id of an object to return as anything but a hash in the format:
{"$oid"=>"4e0005b78ba4db213500001f"}
I've figured out that I'm seeing because I'm getting back a value that's not just an id string but rather of the type BSON::ObjectId. In addition to requiring the rails environment I've also tried requiring bson explicitly in the file that's doing this work:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bson"
I need to get the value simply as a string containing the id, which seems to be the default everywhere else but in this one case. Oddly this behavior only occurs in my dev environment (the rest of the guys on my team don't have this problem).
Requisite disclaimer that I'm new to Mongodb so could be missing something truly obvious.
Thanks!
You could try calling to_s on the object. In irb:
ruby-1.9.2-p180 > p = Project.last #=> #<Project _id: 4e00e77d399a46759d000002, _type: nil, version: 1, created_at: 2011-06-21 18:48:34 UTC, updated_at: 2011-06-21 18:48:34 UTC, name: "Testing MongoDB", client_id: 3, client_name: nil, group_id: 35, requestor_id: 14, requestor_name: "Test Client User", requestor_phone: "", creator_id: 2, creator_name: "Some Guy", manager_id: 23, manager_name: "Some Other Guy", manager_phone: "", manager_email: "", active: true, status: "open", default_hourly_cost: "0.0", default_hourly_charge: "0.0", default_material_markup: "0.35", add_email_internal: "", add_email_client: "", client_po_number: "", client_ticket_number: "", date_requested: nil, date_requested(1i): "2011", date_requested(2i): "6", date_requested(3i): "21">
ruby-1.9.2-p180 > p.id.to_s #=> "4e00e77d399a46759d000002"
If that doesn't work, can you post your environment.rb?

Why am I getting a "SystemStackError: stack level too deep" in my rails3 beta4 model

I'm getting the following error: SystemStackError: stack level too deep when executing the following code in rails3 beta4 under ruby 1.9.2-rc1:
ruby-1.9.2-rc1 > f = Forum.all.first
=> #<Forum id: 1, title: "Forum 1", description: "Description 1", content: "Content 1", parent_id: nil, user_id: 1, forum_type: "forum", created_at: "2010-07-17 04:39:41", updated_at: "2010-07-17 04:39:41", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>
ruby-1.9.2-rc1 > f.children
=> [#<Forum id: 2, title: "Thread 2", description: "Description 2", content: "Content 2", parent_id: 1, user_id: 1, forum_type: "thread", created_at: "2010-07-17 04:40:17", updated_at: "2010-07-17 04:40:17", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>]
ruby-1.9.2-rc1 > f.forum_type = "thread"
=> "thread"
ruby-1.9.2-rc1 > f.save
SystemStackError: stack level too deep
from /Users/emilkampp/.rvm/rubies/ruby-1.9.2-rc1/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
ruby-1.9.2-rc1 >
And that is caused by the following code:
# Before and after filters
#
before_update :update_all_nested_objects, :if => :forum_type_changed?
protected
# Checks if the +forum_type+ has been changed
#
def forum_type_changed?
self.forum_type_changed?
end
# Updates all nested upjects if the +forum_type+ has been changed
#
# This will trigger the +update_all_nested_objects+ method on all touched children, thus
# starting a chain-reaction all the way through the forum-tree.
#
# NOTE: This is where the error is triggered, since this is the only non-tested looping code, and my test-
# cases hasn't changed since this was added.
#
def update_all_nested_objects
children.each do |child|
child.forum_type = child_type
child.save
end
end
So, what's going on. I have been checking around a little, but nobody seems to have the same problem. Either it's not the same ruby version, thus the workflow.rb file is different, or the stack level to deep is caused by something else.
Any help will be greatly apreciated!
Best regards
// Emil
You are calling same method in method itself
def forum_type_changed?
self.forum_type_changed? #This will never ending process hence it gives error
end
I think you have same method name and column name that causing problem change your method name then
def check_forum_type_changed?
self.forum_type_changed? #method name and column name are different
end

Error with "to_sql" on Rails 3 Beta 4

I'm testing Rails 3 beta 4 on Ruby 1.9.2-head, and when I start a
console and do:
Game.first.to_sql
I get this error:
ArgumentError: wrong number of arguments (0 for 1)
I know it can find the Game record, because when I type:
Game.first
it returns:
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37",
updated_at: "2010-06-19 11:02:37">
What am I missing? I just want to make the to_sql work in a very simple
case.
.
When you run Game.first you are returning a Game object, not a ActiveRecord::Relation object as you are expecting.
To do what you're trying to do, you'll need to do:
Game.limit(1).to_sql
This lets you run it without to_sql and return the object as you expected it, although it will be in an array, which then you can run .first on it like you wanted anyways.
irb(main):004:0> Game.limit(1).to_sql
=> "SELECT `games`.* FROM `games` LIMIT 1"
irb(main):005:0> Game.limit(1).class
=> ActiveRecord::Relation
irb(main):006:0> Game.limit(1)
=> [#<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">]
irb(main):007:0> Game.limit(1).first
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">
When you dig into the source, when you run .first on an ActiveRecord::Relation it runs the following (which is the same as I showed you):
def find_first
if loaded?
#records.first
else
#first ||= limit(1).to_a[0]
end
end

Resources