Rails model named Statistics - activerecord

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

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

Polymorphism error in my model

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?

Rails 3 - Scopes

I have models set up similar to this:
class Book < ActiveRecord::Base
has_many :histories, as: :object
end
class Magazine < ActiveRecord::Base
has_many :histories, as: :object
end
class Action < ActiveRecord::Base
belongs_to :object, polymorphic: true
default_scope order(:done_at)
# a history contains an action and the time that action
# occurred on the object it belongs to
end
Now, I want to get a list of the 5 lastest actions that have occurred on all objects. So I can do something like:
Action.limit(5)
However, the problem is that if two actions have recently occurred on the same book, both actions will be retrieved. I want to only retrieve the lastest one. How do I achieve this?
Figured out that what I wanted was the group option. So I can do something like:
Action.group(:object_id).group(:object_type).limit(5)

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 3 relationship definition problem?

I'm new to Rails 3, i'm creating a web app that use active admin, i get a problem with him, and asked for help inside github plugin, someone told me maybe a relationship definitions.
I really dont know what is that, i have nested elements and in active admin i want to make nested element independent.
but now, im totally lost. what i missed? thanks.
here is my model definition
class Company < ActiveRecord::Base
before_save :getsubdomain
has_attached_file :logo, :styles => { :thumb => '150x150>', :medium => '250x250>', :normal => '350x350>'}
has_many :buildings
accepts_nested_attributes_for :buildings
end
Building model
class Building < ActiveRecord::Base
belongs_to :companies
end
in my db, i have colum company_id in buildings table.
Here the error message i get..
NameError in Admin/buildings#index
Showing /Library/Ruby/Gems/1.8/bundler/gems/active_admin-c3a1ffa98072/app/views/active_admin/resource/index.html.arb where line #1 raised:
uninitialized constant Building::Companies
Rails.root: /Users/username/Sites/myapps
Request
Parameters:
{"order"=>"id_desc"}
Response
Headers:
None
thanks for your help
belongs_to expects a singular name. Try
belongs_to :company

Resources