class Reminder < ApplicationRecord
has_and_belongs_to_many :offices, class_name: :failed_offices
end
class Office < ApplicationRecord
has_and_belongs_to_many :reminders
end
class OfficesReminder < ApplicationRecord
belongs_to :office
belongs_to :reminder
end
If I do this in rails console I am getting an error
p=Reminder.last
p.failed_offices
undefined method `failed_offices' for Reminder:0x00007ff87014df78
I want to rename the association for offices. So I tried failed_offices as class_name in Reminder class but it didn't do the trick. Please suggest how it is possible.
Related
I have three models:
class Course < ApplicationRecord
has_many :sections
end
class Section < ApplicationRecord
belongs_to :course
has_many :section_files
end
class SectionFile < ApplicationRecord
belongs_to :section
end
Whenever I try to call #course.sections I receive the error undefined method 'relation_delegate_class' for Course::Section:Module
Any ideas?
Fixed my specifying the class name on Section
Class Course < ApplicationRecord
has_many :sections, class_name: "::Section"
end
Although, #section.section_files seems to work without having to explicitly state the class name
I have 3 models: Topic, Post, Link.
class Topic < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
has_many :links
belongs_to :topic
end
class Link < ActiveRecord::Base
belongs_to :post
end
I'd like to have counter_cache for forums on Link model.
How can I do this ?
Check out the counter_culture gem.
From the readme:
class Product < ActiveRecord::Base
belongs_to :sub_category
counter_culture [:sub_category, :category]
end
class SubCategory < ActiveRecord::Base
has_many :products
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :sub_categories
end
In the above example, the Category model will keep an up-to-date counter-cache in the products_count column of the categories table.
I'm using Rails 4.
I have an Anomaly Records class called Ar that inherits from the following classes as follows:
class RecordBase < ActiveRecord::Base
self.abstract_class = true
end
class ArAndEcrBase < RecordBase
self.abstract_class = true
# Relations
belongs_to :originator, class_name: 'User', foreign_key: 'originator_id'
has_many :attachments
end
class Ar < ArAndEcrBase
end
I want to share some relations with a class that handles another type of records in the Ar subclass however the has_many relationship doesn't work.
The following works:
> Ar.last.originator
=> #<User id: 1, ...
The following crashes:
> Ar.last.attachments
Mysql2::Error: Unknown column 'attachments.ar_and_ecr_base_id'
For some reason the has_many relationship doesn't work well. It should look for column attachments.ar_id and not attachments.ar_and_ecr_base_id
Am I doing something wrong? Or is this a Rails bug?
Atm the only way to get the code working is to move the has_many relation to the Ar class:
class Ar < ArAndEcrBase
has_many :attachments
end
If you want several models to have association to the same other model you probably need a polymorphic association
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
class Employee < ActiveRecord::Base
has_many :pictures, as: :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, as: :imageable
end
I have following active record class
class Car < ActiveRecord::Base
belongs_to :owner
end
in the code when I try this
Car.first.owner
it gives me error "undefined method owner"
Can any one plz let me now if I'm missing any thing
You need to write the relation on the Owner side : has_one :car or has_many :cars depending on your needs.
class Car < ActiveRecord::Base
belongs_to :owner
end
class Owner < ActiveRecord::Base
has_one :car
end
Is it possible to order the results of school.classrooms by the teacher's name? I want to do this directly in the association, and not a separate call.
class School < ActiveRecord::Base
has_many :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :school
belongs_to :teacher
end
class Teacher < ActiveRecord::Base
has_one :classroom
end
This should work if you are using rails 3.x
school.classrooms.includes(:teacher).order("teachers.name")