Polymorphism error in my model - ruby

I'm trying to make a polymorphic model for my reservations.
A reservation can be from a USER to a ROOM, or from a USER to a MENU.
Those are my classes and migrations.
class Room < ActiveRecord::Base
belongs_to :user
has_many :reservations, as: reservable
class Menu < ActiveRecord::Base
belongs_to :user
has_many :reservations, as: reservable
class Reservation < ActiveRecord::Base
belongs_to :user
belongs_to :reservable, polymorphic: true
end
In my controller I'm trying to access my reservations thru:
def your_trips
#trips = current_user.reservations
end
But I'm getting the error:
NameError in PagesController#home
undefined local variable or method `reservable' for # <Class:0x000000054c8308> Did you mean? reset_table_name
Extracted source (around line #4):
Rails.root: /home/ubuntu/workspace
Application Trace | Framework Trace | Full Trace
app/models/room.rb:4:in `<class:Room>'
app/models/room.rb:1:in `<top (required)>'
app/controllers/pages_controller.rb:3:in `home'
Does anyone have ideas why?

Related

Rails polymorphic aliased associations

I would like to try set up this association:
# app/models/course.rb
class Course < ActiveRecord::Base
belongs_to :subjectable, polymorphic: true
end
# app/models/student.rb
class Student < ActiveRecord::Base
has_many :courses, as: :subjectable
end
# app/models/campus.rb
class Campus < ActiveRecord::Base
has_many :courses, as: :subjectable
end
But this did not read very well in the code.
#this seems fine
campus = Campus.last
campus.courses
#this dosent make much sense gramatically
student = Student.last
student.courses
Campuses offer Courses, but Students don't have courses they have subjects. Now they are the same thing under the covers they just don't read well.
How could I get it so that student.subejects would yield the same result as student.courses?
You can name the association as you want, you don't have to mach the associated class.
In this case, you have to tell ActiveRecord what the pointed class is :
# app/models/student.rb
class Student < ActiveRecord::Base
has_many :subjects, as: :subjectable, class_name: 'Course'
end

Rails model named Statistics

I have created following models:
# table - user_statistics
class UserStatistics < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :user_statistics
end
I have also put following rule into inflections.rb
inflect.uncountable %w( statistics )
But when i am trying to access the collection user.user_statistics I am getting an error:
NameError: uninitialized constant User::UserStatistic
What I am doing wrong?
I solved the problem by adding inflect.singular 'statistics', 'statistics' into inflections.rb

Activerecord in rails 3.2 within an engine throws NameError: uninitialized constant when using accepts_nested_attributes_for

I am building a rails engine. I have the following two model classes:
module LandingPageEng
class LandingPage < ActiveRecord::Base
attr_protected # just for debugging right now
has_many :slide_show_images, :dependent => :destroy
accepts_nested_attributes_for :slide_show_images, allow_destroy: true
end
end
The second class is:
module LandingPageEng
class SlideShowImage < ActiveRecord::Base
attr_accessible :image, :landing_page_id
belongs_to :landing_page
validates :image, :presence => true
end
end
The tables associated with them are landing_page_eng_landing_page and landing_page_eng_slide_show_image.
When I run the following in the console I get the error NameError: uninitialized constant SlideShowImage.
1.9.3-p194 :001 > LandingPageEng::LandingPage.new({"title"=>"wd", "tagline"=>"wed", "slide_show_images"=>{"_destroy"=>""}})
NameError: uninitialized constant SlideShowImage
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport- 3.2.6/lib/active_support/inflector/methods.rb:229:in `block in constantize'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `each'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `constantize'
from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
<snip>
I have been banging my head on this and can't figure it out. Any help much appreciated.
I don't have a Rails 3 setup with engines to test this on quickly but I think the issue is with your has_many configuration; it's looking for a class that's named SlideShowImage but your class name is LandingPageEng::SlideShowImage.
I believe adding a :class_name option to your has_many will fix this.
http://railsapi.com/doc/rails-v3.0.8rc1/classes/ActiveRecord/Associations/ClassMethods.html#M004956

NameError: uninitialized constant - Un-allowed syntax in ActiveRecord?

I have two resources that I'm trying to join - Package and Listing through a join table SubmittedPackage. I'm using Ruby 1.9.3-p125 and Rails 3.2.1 with PostgreSQL 9.1.3. The models look as follows.
class Package < ActiveRecord::Base
has_many :submitted_packages
has_many :listings, :through => :submitted_packages
class Listing < ActiveRecord::Base
has_many :submitted_packages
has_many :packages, :through => :submitted_packages
class SubmittedPackages < ActiveRecord::Base
belongs_to :package
belongs_to :listing
In Rails Console I keep getting NameError: uninitialized constant Listing::SubmittedPackage
If I replace the SubmittedPackage resource with Drum it will work (this of course includes having the appropriate table created and so forth).
Is :submitted_packages in conflict with something in Rails or ActiveRecord?
Any ideas why this is breaking?
Thanks in advance!
UPDATE: As a work-around I explicitly defined the :class_name for the has many relationship in the Listing and Package model. This has at least gotten things working, however, it's still is unclear to me why it was necessary to begin with. What Rails or Ruby naming convention was being broken by :submitted_packages ?
class Package < ActiveRecord::Base
has_many :submitted_packages, :class_name => 'SubmittedPackages'
has_many :listings, :through => :submitted_packages
class Listing < ActiveRecord::Base
has_many :submitted_packages, :class_name => 'SubmittedPackages'
has_many :packages, :through => :submitted_packages
class SubmittedPackages < ActiveRecord::Base
belongs_to :package
belongs_to :listing
If SubmittedPackage is the JoinTable, I guess it should have many listings, but I'm seeing just many packages and many submitted_packages.
HTH

rails belongs_to through via association

I'm on rails 3.0 and trying to figure out what would be the proper way to setup a belong_to :through relationship (which) I know is not possible. Here's an example:
class ParentCompany < ActiveRecord::Base
has_many :subsidiaries
has_many :employees, :through => :subsidiaries
end
class Subsidiary < ActiveRecord::Base
belongs_to :parent_company
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :subsidiary
belongs_to :parent_company, :through :subsidiary # <-- I know this is invalid
end
I know I can solve it by doing:
class Employee < ActiveRecord::Base
def parent_company
subsidiary.parent_company
end
end
However, I'd like to know if I can do the above via associations.
You can use delegate to accomplish this without using an association
class Employee < ActiveRecord::Base
belongs_to :subsidiary
delegate :parent_company, to: :subsidiary
end

Resources