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
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 two Models:
User:
class User < ActiveRecord::Base
has_many :comment
Comment:
class Comment < ActiveRecord::Base
belongs_to :movie
has_one :user
end
What I want now is:
that each comment is related exactly to One User
But each user can have mandy Comments ...
But when I want to store it to db I got this error:
Where is my mistake?
On your code:
class Comment < ActiveRecord::Base
belongs_to :movie
has_one :user
end
Try to change:
has_one :user
To:
belongs_to :user
And you can now use, #comment.user = #user.
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
how can I get a weight of product through a Record model? As I know, possible to get all products of certain record but I cannot find out the way getting the weight of certain product.
class User < ActiveRecord::Base
has_many :eatings
end
class Eating < ActiveRecord::Base
belongs_to :user
has_many :records
end
class Record < ActiveRecord::Base
belongs_to :eating
end
class Product < ActiveRecord::Base
end
class WeightedProduct < ActiveRecord::Base
end
What relationships should have Record and Product models with WeightedProduct so user will be able to get weight of certain product through one line User.first.eatings.first.records.first.products.first.weight?
Looks like you're after this:
class Record < ActiveRecord::Base
belongs_to :eating
has_many :weighted_products
end
class Product < ActiveRecord::Base
has_many :weighted_products
end
class WeightedProduct < ActiveRecord::Base
belongs_to :record
belongs_to :product
end
Then User.first.eatings.first.records.first.weighted_products.first.weight
I think that should work but haven't tested.
it seems that each product has one weighted product, then in that case you should add
class Product < ActiveRecord::Base
has_one :weighted_product
end
class WeightedProduct < ActiveRecord::Base
belongs_to :product
end
and
class Record < ActiveRecord::Base
belongs_to :eating
has_many :products
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")