acts_as_nested_set *** ArgumentError Exception: Unknown key: :order - ruby-on-rails-3

Upgrading rails3.2.21 to 4.1.9 and in a class i have use acts_as_nested_set and there is a has_many relation between two class see below code
class Area < ActiveRecord::Base
acts_as_nested_set
has_many :plans
end
class Plan < ActiveRecord::Base
belongs_to :area
end
when i am try to calculate
p = Plan.first
p.area
through an error on terminal like-
ActionView::Template::Error (Unknown key: :order. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table):
how to fix it?

This issue was because i have use the gem
gem 'awesome_nested_set', '~> 2.1.6'
And now i have update the gem file and add gem
gem "awesome_nested_set", '~> 3.0.0.rc.3'
Issue is fixed now

Related

belongs_to with :conditions in Rails 4

I have two models, Users and Accounts:
class User < ActiveRecord::Base
belongs_to :account, :conditions=>proc{" company_account = #{self.company_user} "}
end
class Account < ActiveRecord::Base
has_many :users
end
In Rails 3 belongs_to and :conditions=> works fine, but in Rails 4 I read this options is not valid.
I tried belongs_to :account, -> {where company_account: self.company_user} but I get error undefined methodcompany_user
How can I solve this in Rails 4?
I solved my problem, by using a composed primary key in my Account model.
For this, I used this gem
Try something like:
belongs_to :account, -> { where("company_account_id = ?", self.send(:company_user).id) }

Rails 3 superclass mismatch for class Location

I'm running Rails 3.2.13 and Ruby 2.1.0.
This error has popped up in a seemingly random fashion. I only have one Location class in my app. But I did add several new gems recently: Rmagick, CarrierWave and CarrierWave-Azure.
Here's the error:
TypeError in CompaniesController#show
superclass mismatch for class Location
app/models/location.rb:1:in `<top (required)>'
app/controllers/companies_controller.rb:24:in `show'
If I go to companies_controller.rb ln 24 there is this code:
#addresses = #company.addresses
Line 23: actually references Location:
#locations = #company.locations
If I step through the code in debug mode the #locations variable isn't created anymore when line 23 executes, all other variables prior to line 23 are created. I haven't touched this code in months, the only recent additions to the codebase have revolved around the gems I listed above but did not include changes to Location.rb, Company.rb, Address.rb or Companies_Controller.
Anyone know what's going on here? thx!
Update:
Here is my Location model:
class Location < ActiveRecord::Base
attr_accessible :address_attributes, :address, :created_by, :is_active, :location_name, :location_type_id,
:region_id, :updated_by, :website
# set schema name and table name for TakebackDBMS
self.table_name ="recycle.Location"
# define associations
has_many :companyContacts
belongs_to :location_type
belongs_to :company
belongs_to :address
belongs_to :region
default_scope order: 'location_name' # return locations list in Alphabetical order
accepts_nested_attributes_for :address, :reject_if => :all_blank
#validations
validates :location_name, presence: true, length: { maximum: 100 }
validates :created_by, length: { maximum: 50 }
end
Looks like one of your gems/plugins already defines a Location Class.So is the error.
To resolve this,you should be changing your Location class in your app to some name like Location1
class Location1 < ActiveRecord::Base
This should solve your problem.And don't forget to change your Model file name to location1.rb

Devise : undefined method `user_signed_in? Didn't know why?

Please help i am using rails 3.2.11 and devise 2.0.6 but when i am trying to run my project i am getting an error undefined method `user_signed_in?' for Class
i have done lot of google but didnt find any solution.!
Below is my application trace
NoMethodError in Home#index
Showing /home/amits/RailsWorkspace/Myapp/app/views/home/index.html.haml where line #3 raised:
undefined method `user_signed_in?' for #<#<Class:0xbb1f3f4>:0xbae60b8>
Extracted source (around line #3):
1: %h2= (t :welcome) + " Myapp"
2:
3: - if user_signed_in?
4: = link_to (I18n.t :customers), user_root_path
5: / %li
6: / = link_to (I18n.t :sign_out), destroy_user_session_path, :method => :delete
Rails.root: /home/amits/RailsWorkspace/Myapp
Application Trace | Framework Trace | Full Trace
app/views/home/index.html.haml:3:in `_app_views_home_index_html_haml___806580830_97751900'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
and my gem file is as follows
Gemfile :
source 'https://rubygems.org'
gem 'rails', '3.2.11'
gem 'heroku'
gem 'pg'
gem 'haml'
gem 'haml-rails', :group => :development
gem 'rmagick'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.0'
gem 'uglifier', '>= 1.0.3'
end
gem "devise", "~> 2.0.0"
gem 'devise_aes_encryptable'
# version added so that the gem version will be stable at this point.
gem 'geokit', '1.5.0'
gem 'geokit-rails3', '0.1.5'
My Home controller
Homescontroller.rb
class HomeController < ApplicationController
before_filter :authenticate_user!
def index
end
end
My user model
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :sign_in_count
end
Rotes.rb
Myapp::Application.routes.draw do
devise_for :users
root to: "home#index"
end
Please help me....
Try removing 'clear_helpers' method in ApplicationController as it might block sometimes Devise to load his helpers.

App crashing on heroku with :dependent => :destroy_all

Note everything works in local enviromnment
This is the code
PublicActivity::ORM::ActiveRecord::Activity.class_eval do
attr_accessible :reference_type, :reference_id
has_many :notifications, :dependent => :destroy_all
has_many :users, :through => :notifications
end
This is with the public_activity gem
the error is
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/builder/has_many.rb:20:in `configure_dependency': The :dependent option expects either :destroy, :delete_all, :nullify or :restrict (:destroy_all) (ArgumentError)
if it expects :destroy_all and I wrote :destroy_all and it works locally.. then what is going on here?
To the source!
unless options[:dependent].in?([:destroy, :delete_all, :nullify, :restrict])
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \
":nullify or :restrict (#{options[:dependent].inspect})"
end
So in that error message, the part that says (:destroy_all) is just telling you what you provided; the list of what it was expecting is before that. You probably want :destroy instead. Can't say why it worked locally and not on Heroku; might be some sort of gem version problem.

Writing arbitrary attributes on a model is deprecated. (Multiple belongs_to on model)

I have multiple belongs_to relationships to the same model. Modeling messages between two users as follows (in the Message model):
belongs_to :to, :class_name => 'User', :foreign_key => 'to_id'
belongs_to :from, :class_name => 'User', :foreign_key => 'from_id'
attr_accessible :to, :from # ...
The corresponding has_many calls are in the User model. Everything works in the spec and the console as I need it to, with the exception of the following deprecation warning (for both from_id and to_id):
DEPRECATION WARNING: You're trying to create an attribute `from_id'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer`
The relevant spec follows:
it "can associate users" do
User.delete(:all)
ufrom = FactoryGirl.create(:DrKevorkian)
ufrom.save!
uto = FactoryGirl.create(:JohnSmith)
uto.save!
m = Message.new
m.from = ufrom # <-- Warning here
m.to = uto # <-- Warning here
m.save
m.from.id.should == ufrom.id
m.to.id.should == uto.id
end
It seems to me the warning is happening as a result of the belongs_to association -- is there a cleaner/better way to do this?
Thanks very much.
My experience is that you get this warning if you forgot to run rake db:migrate and rake db:test:prepare after changing your schema.