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.
Related
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.
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")
I have two models one is the Project model and the other one is the ProjectSyndication model, code looks something like this:
class Project
has_one :project_syndication
end
class ProjectSyndication
belongs_to :project
end
ActiveAdmin.register Project, as: 'Offering' do
end
ActiveAdmin.register ProjectSyndication do
belongs_to :offering, parent_class: Project
end
This code gives me an error when I go to the show page of a ProjectSyndication, which is the following:
undefined method `project_syndications' for #<Project:0x007fc309a972b0>
I did just a bit of digging and for the hell of it created an empty project_syndications method in my Project model then got the following error:
undefined method `find' for nil:NilClass
So I can deduce that ActiveAdmin is treating this association like a has_many association and trying to find the ProjectSyndication after finding all the project syndications of one project, which won't work.
I solved the problem by making the following method under the Project model
def project_syndications
ProjectSyndication.where(project_id: id)
end
This works fine, but I feel it is not the right way to do it. I looked through the ActiveAdmin docs, but have not been able to find much. What I asked is for the right way to fix the issue or if this is a bug/feature from ActiveAdmin.
Any help will be appreciated, thank you in advance.
Your question is very vague.
Maybe provide your database migrations as well.
I would check the following (vague guesses):
class Project; has_one :project_syndication; end
=> This inidicates your db has the field projects.project_syndication_id
=> Maybe try "has_many" there?
ActiveAdmin.register Project, as: 'Offering' do; end;
=> Case Offering vs offering below
=> Redundant code? (remove it)
ActiveAdmin.register ProjectSyndication do; belongs_to :offering, parent_class: Project; end
=> Redundant code? (remove it)
I'm following a tutorial and per the code, I should be able to call the following wihtout error:
page = Page.find(1) # works
page.sections.size # Does not work
subject = Subject.find(1) # works
subject.pages.size # works
A section belongs_to a page, and a page belongs_to a subject. I'm trying to count the number of sections that are associated with the respective page (in this case, page :id => 1).
The error is Undefined Method but I'm not accessing a method, I'm accessing an instance variable. I've reviewed my models and controller, and there is no scope or declerations defined for subject.pages.size yet it works without complaint. I'm quite perplexed why it's not working for it's child, page.sections when I'm trying to do the same operation.
The diagnostic info (from CLI using pry) can be found here: http://pastebin.com/xKKvSPkz
DB Schema: http://pastebin.com/hiAhXGt8
Ensure that the relationship between page and section is defined:
class Page < ActiveRecord::Base
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :page
end
With this relationship the following should work as expected:
page = Page.find(1)
page.sections.size
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