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
Related
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.
I have following models in rails.
class User < ApplicationRecord
has_many :vendors
has_many :vendoritems, through: :vendors
has_many :products
end
class Vendorcode < ApplicationRecord
has_many :vendoritems
end
class Vendoritem < ApplicationRecord
belongs_to :vendorcode
belongs_to :vendor
end
class Vendor < ApplicationRecord
belongs_to :user
has_many :vendoritems
end
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
has_many :vendoritems, XXXXX
end
Product has many vendoritems through vendorcode and user.
How can I implement this association.
I'd just go for an instance method like so
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems
end
end
Cheers!
class Product < ApplicationRecord
belongs_to :user
belongs_to :vendorcode
def vendoritems
user.vendoritems.where('vendorcode =?', vendorcode.id)
end
end
I solved the problem.
Suppose we have this contrived model structure
class Apple < ActiveRecord::Base
belongs_to :fruit
has_one :tree, through: :fruit
has_one :organism, through: :tree
end
class Fruit < ActiveRecord::Base
belongs_to :tree
has_many :apples
end
class Tree < ActiveRecord::Base
belongs_to :organism
has_many :fruits
end
class Organism < ActiveRecord::Base
has_many :trees
end
To avoid having to call #apple.fruit.tree.organism, I have definded the two has_one-through directives in Apple, and expect #apple.organism to work, but it does not. #apple.tree.organism does work.
Am I doing something wrong? Should I just define a getter method for :organism on Apple instances and be done with it?
Your has_one technically performs the characteristic of belongs_to: think Organism has_many apples through trees and not the other way around. "belongs_to :through" does not work in Rails, so I suggest using delegate in your models.
class Apple < ActiveRecord::Base
delegate :organism, to: :fruit
end
class Fruit < ActiveRecord::Base
delegate :organism, to :tree
end
The association between class are as below
class Level < ActiveRecord::Base
has_many :levels_users
end
class LevelsUser < ActiveRecord::Base
belongs_to :level
has_many :schedules
end
class Schedule < ActiveRecord::Base
belongs_to :levels_user
end
I know the value of level.id. How do I find all schedules belonging to that level.id. level.id is the id of one of the level. I tried something like this:
Level.find(level.id).levels_users.schedule
But it won't work.
You could use through property of has_many association.
class Level < ActiveRecord::Base
has_many :levels_users
has_many :schedules, through: :levels_users
end
Now you can use
level.schedules
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