Problem running thumbs_up gem in Rails 3 - ruby-on-rails

I followed the gem installation steps on https://github.com/brady8/thumbs_up but ran into an error when following the usage steps.
This is taken from my User and Comment models:
class User < ActiveRecord::Base
<some devise specific stuff>
acts_as_voter
end
class Comment < ActiveRecord::Base
acts_as_voteable
end
The error I'm getting is the following:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in 'method_missing'Exiting
: undefined local variable or method 'acts_as_voter' for # (NameError)
Removing the acts_as_voter line in User model eliminates the problem even though there is an 'acts_as_voteable' in the Comment model which seems to work fine then.
Any ideas?

I had the same issue after adding the thumbs_up gem to my application.
Restarting the server cleared it up for me.

Related

Uninitialized constant Ahoy::QueryMethods - Ahoy Gem

I'm using the Ahoy Gem (https://github.com/ankane/ahoy) to track visits and events. When I try to use their where_event method to query the event model as described in the docs I get this error:
[191] pry(main)> Ahoy::Event.where_event("Reach", business_id: 4072).count
NameError: uninitialized constant Ahoy::QueryMethods
from /Users/rfrisch/projects/impact/app/models/ahoy/event.rb:2:in `<class:Event>'
event.rb
class Ahoy::Event < ActiveRecord::Base
include Ahoy::QueryMethods
self.table_name = "ahoy_events"
belongs_to :visit
belongs_to :user
end
As Ahoy::QueryMethods is provided by the gem I'm not sure how to go about addressing this error.
If I comment out the include line on event.rb then I can properly record events in the table but then I lose the ability to use the where_event method.
Any help getting this to work would be appreciated.
Turns out I need to actually restart the console vs. just running reload!. Now all works as expected.

Rails uninitialized constant name error

I keep getting this error:
NameError (uninitialized constant Character::Messagemissife):
uninitialized constant Mime::HTML
The error is coming from this line:
if #character.messagemissives
character.rb
has_many :messagemissives, dependent: :destroy
messagemissive.rb
class Messagemissive < Missive
self.table_name = 'messagemissives'
belongs_to :character
end
missive.rb
class Missive < ActiveRecord::Base
self.abstract_class = true
end
I have a class Messagemissive, but not Messagemissife. Of course, it looks like a typo error. But I can't find "Messagemissife" anywhere in any of my files. I've used the Find function in Sublime Text 2, I've used the mac Finder search, I've cleared the cache, I've restarted the server several times, I've restarted the computer several times. Still this error won't go away. What am I doing wrong?
You are seeing this behaviour because of rails's default naming convention. When you call #character.messagemissives, rails is actually looking for a model with it's corresponding singular term Messagemissife and not Messagemissive. You can confirm this by typing"Messagemissives".singularize
in rails console which will return you "Messagemissife".
To fix this issue, either you can mention the class name with association like
has_many :messagemissives, class_name: 'Messagemissive'
or as mentioned here, in /config/initializers/inflections.rb, just add
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'messagemissive', 'messagemissives'
end
Hope this will help.

Manually adding data to a namespaced model in rails

I'm trying to manually add data to a table in rails using the rails console, but I keep getting a undefined local variable or method error.
The model is namespaced under ratings:
models/ratings/value_for_money_score.rb
class Ratings::ValueForMoneyScore < ApplicationRecord
end
To try and manually add a record to the database, I run the following in the console:
$ RatingsValueForMoneyScore.create(score: '1', description:"Terrible, definitely not worth what was paid!")
And I get this error: NameError: uninitialized constant RatingsValueForMoneyScore
I have tried a few different versions such as
RatingsValueForMoneyScores.create,
Ratings_ValueForMoneyScores.create,
ratings_value_for_money_scores.create but keep getting the same error. What am I doing wrong?
Try
::ValueForMoneyScore.create(score: '1', description:"Terrible, definitely not worth what was paid!")
The error is descriptive enough in this case, the class RatingsValueForMoneyScore pretty much doesn't exist. Check your namespace, you have a class name (which should be singular) Rating, and the module ValueForMoneyScore. You'd use either
(after renaming the class to Rating)
Rating.create(...)
or
ValueForMoneyScores.create(...)
Your syntax is equivalent to:
class Rating
module ValueForMoneyScores < ApplicationRecord
...
end
end
The answer was a combination of input, so collecting that in one answer.
My namespaces weren't properly set up (I was using "Ratings" rather than "Rating"), so I fixed this for the tables. My models then looked as follows:
models/rating.rb
class Rating < ApplicationRecord
end
models/rating/value_for_money_score.rb
class Rating::ValueForMoneyScore < ApplicationRecord
end
And then this command worked for creating records in the rating_value_for_money_score table
$ Rating::ValueForMoneyScore.create(score: '1', description: "Test")

NoMethodError: undefined method 'admin_users'

I'm new to rails so I'm working on the lynda.com Ruby on Rails 5 Essential Training. I got to section 6 the last part titled "Traverse a rich association". From the rails console I used the command section.admin_users and got
NoMethodError: undefined method `admin_users' for #<Section:0x000000033e76c0>.
I followed the tutorial and even compared my code to the example files and see no reason I may be getting this error. I have a github repo with all the code. Not sure why I get this.
As Sharvy suggested restarting the console fixed my issue.
Above, you say you queried for this section.admin_user
In the section model:
class Section < ApplicationRecord
belongs_to :page
has_many :section_edits
has_many :admin_users, :through => :section_edits
end
Section has many admin users. Try to modify your query to:
section = Section.first
section.admin_users
This is assuming you have a Section object in the database.

Acts_as_taggable_on fails in rails 3.1

I am using acts_as_taggable_on on my rails 3.1 app. This is my model
class User < ActiveRecord::Base
acts_as_taggable_on :skills
end
I get this error the moment I try anything with the user object.
NoMethodError: undefined method `acts_as_taggable_on' for #<Class:0x90bfd84>
I even tried just using acts_as_taggable but didnt work. Am i doing anything wrong or is this a known issue?
I am using acts_as_taggable_on with rails 3.1 and it works ok. I am using acts_as_taggable 2.1.1
I defined acts_as_taggable in model I want to tag (ie Books) and acts_as_tagger in model that acts as tagger (ie User).
Gem documentation may be confusing because it tags users in the examples. ¿Do you want to tag users or another thing?
Did you make Post Installation steps?
rails generate acts_as_taggable_on:migration
rake db:migrate
In my case looking to table in database used by acts_as_taggable_on was helpful
https://github.com/mbleigh/acts-as-taggable-on/blob/master/generators/acts_as_taggable_on_migration/templates/migration.rb
Hope this helps

Resources