I have
gem 'groupdate'
in my Gemfile in my Rails project. I run bundle and then open bin/rails c to test the methods out yet I still get the error:
[4] pry(main)> users.group_by_day { |u| u.created_at }
NoMethodError: undefined method group_by_day' for #>>><User::ActiveRecord_Relation:0x007faee79ca818>
from /Users/Jonathan/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:136:inmethod_missing'
I've ran bundle update and whatnot and I'm unsure what the issue could be
Related
I am using rake with sinatra and activerecord (not rails). I have two model classes - Admin (in admin.rb) and Bill (bill.rb). I was able to run rake db:create_migrations with the following output (but I cannot see any tables in the database - sqlite). Any idea what I might be doing wrong?
== 20150907135939 CreateAdmins: migrating ======================================
-- create_table(:admins)
-> 0.0011s
== 20150907135939 CreateAdmins: migrated (0.0012s) =============================
== 20150907140840 CreateBills: migrating ======================================
-- create_table(:bills)
-> 0.0005s
== 20150907140840 CreateBills: migrated (0.0006s) =============================
Rakefile
# Rakefile
require './app'
require 'sinatra'
require 'active_record'
require 'sinatra/activerecord/rake'
Gemfile
# Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem 'sinatra-flash'
gem 'sinatra-redirect-with-flash'
gem 'rake'
gem 'sqlite3'
=begin
group :development do
gem 'sqlite3'
gem "tux"
end
group :production do
gem 'sqlite3'
end
=end
When I run the following query in a test.rb file I get an exception:
puts("type email address of admin")
admin_email = gets.chomp
find_admin = Admin.find_by(email:admin_email) # read
puts("admin retrieved with email: #{find_admin.email} and name: #{find_admin.name}")
Exception:
test.rb:41:in `<main>': undefined method `email' for nil:NilClass (NoMethodError)
Object I get back is nil (I checked using find_admin.nil?)
First of all, try to strip the leading and trailing whitespaces after you take the input email, like this:
admin_email = gets.chomp.strip
puts "admin_email: #{admin_email.inspect}"
Then, try your script again. It should work.
To explain what's going on here, your query:
find_admin = Admin.find_by(email:admin_email)
this can't find an admin record with the provided email. So, it's a nil object and when you call: find_admin.email i.e. nil.email then you get the error:
undefined method `email' for nil:NilClass (NoMethodError)
You need to make sure you have an admin with the given email. To do that, you can create one first, in your rails console, do this:
Admin.create!(email: 'testadmin#test.com')
Then, try to find the admin like above by giving testadmin#test.com as input.
It should work if the admin record with the given email is present in the database.
We are upgrading Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:
s.add_dependency 'activerecord-session_store'
and added following in initializers/session_store.rb under dummy:
Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'
then, we did bundle install. When we ran:
bundle exec rails generate active_record:session_migration
There is the error from the gem's generator:
/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).
We move the gem into engine's Gemfile and same error. Why the SessionStore is still not initialized?
Update
In engine's engine.rb under lib, the session table is pointed to:
initializer "Authentify.add_middleware" do |app|
ActiveRecord::SessionStore::Session.table_name = 'authentify_sessions'
app.middleware.use ActiveRecord::SessionStore
end
The setup works for Rails 3.2.
If you were not using the default table name for sessions, set:
ActiveRecord::SessionStore::Session.table_name = 'your_old_session_table'
in config/application.rb.
Additional configuration.
What we did is to add gem 'activerecord-session_store' to engine's Gemfile in addition to the .gemspec. The error disappeared.
I have come across an issue with my gem, it seems that it is not initializing when starting the rails server, if i run this script in the console i can access the model classes, however when i start the rails server i get 'uninitialized constant errors'
require "blogModels/version"
module BlogModels
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "models/#{filename}"
end
end
my gemfile
gem 'mygem', :git => "pathtogem"
afterwhich i ran bundle
Would there be a reason as to why it is not loading when i am retrieving it from git?
Thanks
I would like to know if when using pry is possible to have access to the variable app?
As an example, when I try to access the root_path I get the following error:
[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object
Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.
I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
Yes, it works
➜ MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"
I use pry-rails in favor of your overriding of IRB in an initializer.
group :development do
gem 'pry-rails'
end
https://github.com/rweng/pry-rails
Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
You can read more in the pry wiki
Im trying to install Kaminari pagination on rails 3 with adminpanel RailsAdmin, but I get this error:
NoMethodError in ShowsController#
undefined method `page' for # < ActiveRecord::Relation:0xaadc8d4>
Do you also have the gem "will_paginate" in use?
Check the file Gemfile.lock to see if you have this gem in use as well:
grep will_paginate Gemfile.lock
If that's the case, all you have to do is to create the file "config/initializers/kaminari.rb" and write this content on the file:
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
end
that should fix the issue
In my case I forgot to add the kaminari gem to my Gemfile :facepalm: