Newbie rails developer here.
I'm having an issue trying to setup some very simple database associations in a new rails project.
In my database I have two tables, one called "Games" and one called "Onlines". Here's what's in them right now
Game.first
#<Game id: 1, name: "Game 1", description: "This is a cool game", url: "http://domain.com">
Online.first
#<Online id: 1, game_id: 1, now: 222>
I'm trying to setup a simple association so I can grab the number of users online by doing something like...
Game.find(1).onlines.now
In my game.rb and online.rb models I have
belongs_to :online
and
belongs_to :games
accordingly.
When I try and run Game.find(1).onlines.now in the rails console I get the following error.
NoMethodError: undefined method `onlines' for #<Game:0x00000101654300>
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/activemodel-3.0.6/lib/active_model/attribute_methods.rb:367:in `method_missing'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:46:in `method_missing'
from (irb):5
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Jon/.rvm/gems/ruby-1.9.2-p180#rails3tutorial/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Am I missing something ridiculously obvious here? I've tried everything I can think of.
You should have
has_many :onlines
in your Game model, not belongs_to.
Related
I'm able to do the bare bones keyword search with no issues in my app using elasticsearch, but as_indexed_json isn't working apparently and I have no idea why. I'm following this tutorial, and the section in question is almost halfway down.
Please let me know if I need to provide any additional info. I'm pretty new to this, so I apologize if this is a really dumb question.
Rails console:
2.2.4 :011 > Term.first.as_indexed_json
Term Load (0.4ms) SELECT "terms".* FROM "terms" ORDER BY "terms"."id" ASC LIMIT 1
NoMethodError: undefined method `as_indexed_json' for #<Term:0x007fa6cd9a6408>
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.5/lib/active_model/attribute_methods.rb:433:in `method_missing'
from (irb):11
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Model:
require 'elasticsearch/model'
class Term < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def as_indexed_json(options = {})
as_json(
only: [:name, :definition, :etymology1, :etymology2, :uses, :romance_cognates, :notes1, :notes2, :quote1, :quote2]
)
end
end
Based on that tutorial i hope you opened the irb using rails c.
But open console like this bundle exec rails c and try again
I'm trying to create an Active Record anonymous association extension has the documentation explains here. And I've got this models
class Program < ActiveRecord::Base
has_many :reports do
def asdf
"Hello extension"
end
end
end
class Report < ActiveRecord::Base
belongs_to :program
end
But when I test this on my rails c console I've an undefined method error.
Loading development environment (Rails 4.2.5)
2.1.0 :001 > Program.first.reports.asdf
Program Load (0.3ms) SELECT `programs`.* FROM `programs` ORDER BY `programs`.`id` ASC LIMIT 1
NoMethodError: Report Load (0.3ms) SELECT `reports`.* FROM `reports` WHERE `reports`.`program_id` = 1
undefined method `asdf' for #<Report::ActiveRecord_Associations_CollectionProxy:0x000001035e0098>
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):1
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
I'm not trying to share the same extension between many associations, just trying to make this work through the anonymous module (block).
Any ideas?
That's because you retrieved many reports and called the method asdf on them, whereas your method asdf is defined for a single report.
Try this
Program.first.reports.first.asdf
(Rails 4.2.4) Hello, beginner here. I am working on a project which does not need a DB or activeRecord. Therefore, when making my Rails project I appended the -O (to disable Active Record and database) (rails new MyApp -O)
I read that to do a model not backed by a database you can just create a file in
app/models/site.rb.
No need to do:
rails generate model Site
So I added my model, which looks something like this:
class Site
attr_reader :name
attr_reader :out_average
attr_reader :in_average
attr_reader :change
def initialize(name, in_average, out_average)
#name = name
#out_average = out_average
#in_average = in_average
#change = find_increase
end
def find_increase()
if #in_average && #out_average != 0
#change = ((#in_average - #out_average)/#out_average)*100
else
#change = 0
end
return #change
end
end
So, I then started up console "rails c" and when I try to invoke a new Site object, I get an error:
irb(main):001:0> Site.new
NameError: uninitialized constant Site
from (irb):1
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ms-87/Documents/projects/rails_projects/site_seasonality/bin/rails:8:in `<top (required)>'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
I made sure I started the console from the root of my app. I also made sure to use the proper naming convention (site.rb is the filename in app/model/, "Site" is the name of my class inside the file). Can anyone help me as to why this isn't working? Is my thinking here wrong? Thanks.
My first error was that my filenames were capitalized "Site.rb", I had actually fixed this before I posted. But after I fixed it, I accidentally started using "irb" instead of "rails c". DOH! Sorry for the post.
Hello i'm a beginner following the Lynda ruby on rails tutorial.
Here is the code I have to run subject = Subject.new. but everytime i type that into the rails console, I get this error.
NameError: uninitialized constant Subject
from (irb):1
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands/console.rb:90:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands/console.rb:9:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
new is a method of Class. With your code
subject = Subject.new
you want to create an instance of the class Subject, but you it seems that you haven't defined a class Subject.
On irb you can do it this way:
class Subject
end
and now with
subject = Subject.new
you can create an instance of Subject, what you can test with
subject.class
=> #<Subject:0x007fca538325c8>
But this all makes not much sense if you haven't defined any methods for the class Subject. And this is all is essential ruby or essential OOP, so try to get some basic stuff about Ruby. I suggest Rubymonk or something else.
I am sure its a basic problem in RoR but I added a new table called hooods_one_providers. This table has no corresponding model - It should connects two models - Providers and Hoods. I am trying to call it in the console but getting instead - uninitialized constant.
When I run:
ActiveRecord::Base.connection.tables
=> ["schema_migrations", "users", "roles", "users_roles", "providers", "food_items", "food_items_users", "feedbacks", "addresses", "carts", "link_carts", "hoods", "drink_items", "addons_ons", "addons_nears", "customize_foods", "addresses_hoods", "hoods_one_providers"]
I can see the table but I cant read from it. When I run hooods_one_providers i get the uninitialized constant error:
NameError: uninitialized constant HoodsOneProvider
from (irb):14
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/ido/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
In the model provider I have:
has_and_belongs_to_many :hoods_one, class_name: 'HoodsOne'
And in the model hood I have:
has_and_belongs_to_many :providers
Will appreciate any help.
Thanks
This could happen for any one of the cases below
Running reload! should fix.
Exit the console and run console again by rails c
Make sure the model file exists for the respective table. Let's assume your table name is hoods_one_providers then there should be hoods_one_providers.rb file under the model.
if you've added the table while the console is running, the classes are already cached by the console. Running reload! should fix the issue but in case it doesn't, a restart of the console should.
UPDATE:
You are using has_and_belongs_to_many which needs you to manually create the joins table.
UPDATE: creating the joins table
create a migration which contains the following to create the joins table. the id: false option tells it not to create an id column
create_table :hoods_ones_providers, id: false do |t|
t.references :hoods_one, :provider
end
add_index :hoods_ones_providers, [:hoods_one_id, :provider_id]