I have the following two models
class ContactField < ActiveRecord::Base
end
class Address < ContactField
end
class Phone < ContactField
end
and
class Contact < ActiveRecord::Base
end
class Company < Contact
end
class Person < Contact
end
I want one contact, no matter is it Company or Person, to have many ContactFields(Addresses and Phones)... So where should I put those has many and belongs to?
Thanks
You already said it in plain english :-)
I want one contact, no matter is it Company or Person, to have many ContactFields(Addresses and Phones)... So where should I put those has many and belongs to? Thanks
class Contact < ActiveRecord::Base
has_many :contact_fields
end
class ContactField < ActiveRecord::Base
belongs_to :contact
end
This Relationship will be inherited by both address and phone
Looks like you're describing a belongs to relationship. The associations should be defined in the parent class, so they can be inherited by the subclasses.
class ContactField < ActiveRecord::Base
belongs_to :contact
belongs_to :company, :foreign_key => :contact_id
belongs_to :person, :foreign_key => :contact_id
end
class Contact < ActiveRecord::Base
has_many :contact_fields
has_many :addresses
has_many :phones
end
However #contact.contact_fields will just return ContactField records. If you need the methods defined in any of the sub classes you can always use the becomes method. There are a few ways around that. Such adding the extra associations, like I did. Or using ActiveRecord::Base#becomes
Related
I have a table called foods and categories, but this table has no relation, I want to connect them through another table called food_category. And I want to make one-to-one relation between food and category maybe the diagram looks like this
class Food < ApplicationRecord
has_one :category
end
class Category < ApplicationRecord
has_one :food
end
class FoodCategory < ApplicationRecord
belongs_to :category
belongs_to :food
end
Is it possible to do this?
Yes, this is possible. You just need to do
has_one :category, through: :food_categories
as discussed in the Rails docs here.
However, this is a long-winded way to go about this kind of association. If it's going to be one-to-one, why not just add a foreign key to Category from Food? And presumably, you would actually want Category to contain many Food records? Seems like the below would make more sense:
class Food < ApplicationRecord
belongs_to :category
end
class Category < ApplicationRecord
has_many :food
end
class Food < ApplicationRecord
has_one :food_category
has_one :category, through: :food_categories
end
In rails console, you can access like this
Food.find(:id).categories
In my app I have 2 classes. User and Classroom. I use the user class as a student as well.
I'm trying to achieve a result where:
A classroom belongs to a user.
A user has many classrooms.
A classroom has one student through the user class.
A student can be associated to many classrooms.
To try and explain further. I have a classroom and the user is the creator of the classroom. When someone joins they are a student of the classroom and I only want there to be one student and one creator.
I want a student to be attached to lots of different classrooms and I want the classrooms to all belong to one user.
My current code for the two classes looks like this:
class User < ActiveRecord::Base
has_many :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :user
has_one :student, :class_name => "User"
end
Any advice is much appreciated. Thanks!
I think what you are trying to achieve is:
class User < ActiveRecord::Base
has_many :classroom_users
has_many :classrooms, through: :classroom_users
end
class ClassroomUser < ActiveRecord::Base
belongs_to :classroom
belongs_to :user
end
class Classroom < ActiveRecord::Base
has_many :classroom_users
has_many :users, through: :classroom_users
end
What I want to do is to get all the
My model:
class User < ActiveRecord::Base
has_many :classes
has_many :professors, :through=>:classes
has_many :cars
has_many :carmodels, :through=>:cars
end
class Professor < ActiveRecord::Base
has_many :classes
has_many :users, :through=>:classes
end
class Class < ActiveRecord::Base
belongs_to :user
belongs_to: professor
end
class Car < ActiveRecord::Base
belongs_to :user
belongs_to :carmodel
end
class Carmodel
has_many :cars
has_many :users, through=>:cars
end
what I want to do is, given a certain Car and Professor, to find all users which contain them.
for example
u1=carmodel.users
u2=professor.users
result=[]
u1.each do |us|
if u2.include? us
result.push us
end
end
Of course this is just an example... I would like to keep working with ActiveRecords(avoid turning it to an array) and, of course, a more optimal solution... I can't seem to find any.
You need to do something like this:
User.joins(:carmodels).joins(:professors)
Thanks for the previous answer! Here is the resulting code:
carmodel #Variable for the wanted carmodel
professor #Variable for the wanted professor
result=User.joins(:carmodels).joins(:professors).where(carmodel:{id:carmodel.id},professors:{id:professor.id})
Thanks again!
I have 2 models and one linking them in many-to-many relation.
Like this:
class Family < ActiveRecord::Base
has_many :family_in_house
end
class House < ActiveRecord::Base
has_many :family_in_house
end
class FamilyInHouse < ActiveRecord::Base
belongs_to :family
belongs_to :house
end
And I need to have a separate scope for families that are not associated with any house.
I'm realtively new to RoR and couldn't find the solution myself. I use Rails 3.2.9 if it's necessary.
Thanks in advance!
class Family < ActiveRecord::Base
has_many :family_in_houses
scope :without_house, includes(:family_in_houses).where( :family_in_houses => {:house_id=>nil} )
end
We have the following (part) models.
User
Booking
Apartment
Villa
Bed_Breakfast
Boutique_hotel
The user can have many bookings and a booking can have many villas, apartments, B&B or boutique hotels.
This is my idea to set up the relationships.
class User
has_many: bookings
end
class booking
belons_to :user
end
class Apartment
belongs_to :booking
end
class Villa
belongs_to :booking
end
Is this the right way to do it?
Thanks...
remco
I think, your approach is not good. Seems you are looking for has_many :through association in your tables. you can have a look into Rails has_many :through Docs. you should go like this:
class User
has_many :apartments, through: bookings
end
class booking
belongs_to :user
belongs_to :apartment
end
class Apartment
belongs_to :user
has_one :booking
end
Hope it will help. Thanks.
Yeah everything looks good and add this
class booking
belongs_to :user
has_many :villas
has_many :apartments
...
end
Villa and Apartment seem to be the same thing. Are they not both a Property? You could have a Property to keep yourself DRY (don't repeat yourself) which would eliminate writing the same code for each of the Apartments/Villas. My recommendation is this:
class Villa < Property < ActiveRecord::Base
end
class Apartment < Property < ActiveRecord::Base
end
Alternatively you could use a Polymorphic Design and have a property type.