I've 2 classes with a has_and_belongs_to_many relation.
When I try to destroy an object I get:
> undefined method `name' for nil:NilClass error.
I'm using Ruby 2.2.2. The same code works fine with Ruby 2.1.2.
My controller code:
#cart = Cart.find(1)
#cart.temp_orders.find(4).destroy
My models:
class Cart < ActiveRecord::Base
has_many :temp_orders
end
class TempOrder < ActiveRecord::Base
belongs_to :cart
has_and_belongs_to_many :kids, join_table: :kid_temp_orders
end
class Kid < ActiveRecord::Base
has_and_belongs_to_many :temp_orders
end
Stack trace:
> NoMethodError (undefined method `name' for nil:NilClass): app/controllers/carts_controller.rb:50:in `destroy'
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.8ms)
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (29.3ms)
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.7ms
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (123.5ms
OP indeed can't post any other data for this error. I think OP doesn't deserve those down votes in this case.
This is an issue with ActiveRecord and Ruby 2.2
You can fix it by switching the ruby version from ruby-2.2.0 to ruby-2.1.2/ruby-2.1.3 or rails version from 4.0.0 to 4.1.2
Check this SO link
upgrading from:
ruby '2.3.0'
gem 'rails', '4.1.1'
to:
ruby '2.3.0'
gem 'rails', '4.1.16'
did the trick for me
Related
I am using 'acts_as_messagable' gem for private messaging between users.
My model:
class User < ActiveRecord::Base
acts_as_messageable :table_name => "messages"
..
end
I have error on start application:
`method_missing': protected method `default_scope' called for #<Class:0x007f84d7870da8> (NoMethodError)
current rails version 4.1, migrations are done.
What is the root of problem?
It's not compatible from the docs:
the gem is compatible with Rails 2.3, an upgrade to Rails 3 is planned
I'm trying to add Searchkick to my Rails application. I'm following the exact instructions on the Get started page but I keep getting the following error:
Started GET "/airports" for 10.0.2.2 at 2014-05-26 10:20:33 +0000
Processing by AirportsController#index as HTML
Completed 500 Internal Server Error in 26ms
NameError (undefined local variable or method `searchkick' for #<Class:0xb48f3ba8>):
app/models/airport.rb:2:in `<class:Airport>'
app/models/airport.rb:1:in `<top (required)>'
app/controllers/airports_controller.rb:3:in `index'
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.8ms)
Here's what the Airport model looks like:
# ./app/models/airport.rb
class Airport < ActiveRecord::Base
searchkick
end
And this is what the Airport controller looks like:
# ./app/controllers/airports_controller/rb
class AirportsController < ApplicationController
def index
#airports = Airport.all
end
end
What's causing this?
Restart the server. The server has to be restarted after a change is made to the Gemfile.
Other things you can try if that doesn't work:
add gem 'searchkick' to the Gemfile
bundle install
Same here. I checked gem list, searchkick was there.
But I deleted searchkick in Gemfile and runned bundle install. Then add searchkick and bundle install again, it works well:)
Using the serialize_with_options gem in rails 3 application.
gemfile
gem 'serialize_with_options'
in model (User.rb)
serialize_with_options(:less) do
only :id, :name
end
there is an exception thrown when application is booted:
NoMethodError: undefined method `serialize_with_options' for User
any clue why/how rails is not loading gem?
I'm getting this strange error using Rails 3.0.2.
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
This is the controller, and line 19 is the respond_with(#channels) block.
Where do I start to search for errors?
class ChannelsController < ApplicationController
before_filter :set_default_client
respond_to :html, :xml
def index
if params[:cache_set]
#channels = Channel.active.find_all_by_id(params[:cache_set])
else
#channels = Channel.active.find_all_by_id(cookies[:channels].split(','))
end
respond_with(#channels)
end
end
This is the full error:
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (19.8ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (28.6ms)
I'm using Ruby 1.8.7 with Rails 3.0.2.
I've also, just in case, tried Rails 3.0.7 and 3.0.0.
Are you in any case using HAML?
I just bumped into this as well and my colleague (who is not on Stackoverflow yet) found out that it was due to multiline comments in the view template
-#
= helper_method_1
= helper_method_2
I solved the problem by changing HAML version from 3.1.x to 3.0.24.
My new Gemfile looks like this.
gem "rails", "3.0.2"
gem "haml", "3.0.24"
gem "compass", "0.10.6"
It looks like HAML-edge has fixed this problem.
I expect the next release of HAML after 3.1.1 to resolve this.
Does anyone know why this callback method would work in Rails 2.3.x / Ruby 1.8.7 but not in Rails 3.0.x / Ruby 1.9.2?
Works in Rails 2.3.x / Ruby 1.8.7:
class Post < ActiveRecord::Base
after_create :destroy_old_posts
acts_as_taggable
# ...
protected
def destroy_old_posts
self.tag_list.each do |tag|
posts = Post.find_tagged_with(tag, :order => 'updated_at DESC')
posts[19..-1].each {|p| p.destroy } if posts.size >= 20
end
end
end
But with 3.0.x / Ruby 1.9.2 I receive the following error upon creating a post:
undefined method `find_tagged_with' for #<Class:0x00000002943048>
app/models/post.rb:30:in `block in destroy_old_posts'
app/models/post.rb:29:in `each'
app/models/post.rb:29:in `destroy_old_posts'
app/controllers/posts_controller.rb:29:in `block in create'
app/controllers/posts_controller.rb:28:in `create'
I'm using acts-as-taggable-on 2.0.6. Thank you for reading my question.
I believe the correct usage is Post.tagged_with