Sorry, I am not very advanced in mapping out databases
I have a model similar to this: A teacher can have many students, and a student can have many teachers. So How would I make this? If a student could have only one teacher I know I would set an attribute like: teacher_id: integer, then when I want to create a student it would be similar to this
Student.create(:teacher_id => id)
or query similar to this:
Student.where(teacher_id: id)
Teacher.find(student.teacher_id)
But I am unsure of how to accomplish this if both are has_many relationships
You can use rails has_and_belongs_to_many relationship for your requirement. check this link for reference: http://guides.rubyonrails.org/association_basics.html#has-and-belongs-to-many-association-reference
you need to use this relationship like this:
teacher.rb
has_and_belongs_to_many :students
student.rb
has_and_belongs_to_many :teachers
then you need to add a migration to create a join table containing teacher_id and student_id
you should create name of your migration in alphabetical order like this:
rails g migration create_join_table_for_students_teachers student_id:integer teacher_id:integer
and then rake db:migrate
then you can access students of a single teacher like teacher.students etc.,
Hope this might help you in some way please go through the reference link once
You're describing a has-and-belongs-to-many (HABTM) relation, which Rails supports in two ways: has_and_belongs_to_many or has_many :through. You can read about them in the Active Record Associations Rails Guide, which also gives you tips and how to choose which one is appropriate for your application.
Better use has_many:through relation.
Related
Which is the best way to do database migration when association changes?
For example:
Add has_one/ has_many/ & belongs_to association to 2 models have no association.
Add has_many_and_belongs_to association to 2 models have no association.
Add has_one/ has_many/ & belongs_to association to 2 models that one model have association with other model, another model is newly created.
When your association changes, is it not just that you have to change the foreign keys in the db scheme and create specific migrations for that?
For example if you want to add a has_many relationship to store which has many books. You could easily create a migration with the generate command.
rails g migration addStoreIdToBooks store_id:integer
Then in your model/store.rb
has_many :books
And in the model/book.rb
belongs_to :store
Otherwise I didn't understand your question :)
I'm working with Rails 3.2.1 and have the two models CookingVenue and DiningVenue with associated MySQL tables of cooking_venues and dining_venues. I have set up the has and belongs to many relationship between the two models but what's the name of the MySQL table name here to represent the join?
Is it cooking_venues_dining_venues?
Will Rails try to find habtm relationships between cooking and venues etc, or is Rails really clever enough to work all this out?
Like you said, cooking_venues_dining_venues is the name of the join table. After creating this table with cooking_venue_id and dining_venue_id field you need to define has_and_belongs_to_many association in both model.
class CookingVenue < ActiveRecord::Base
has_and_belongs_to_many :dining_venues # foreign keys in the join table
end
class DiningVenue < ActiveRecord::Base
has_and_belongs_to_many :cooking_venues # foreign keys in the join table
end
Yup. You just name the join table in alpha order like you have done and you should be good to go.
I'm quite new to this and I'm using cancan + devise for my user auth. However I'm not really sure what it means to set up a typical users HABTM roles relationship nor do I really understand what a HABTM relationship is.
Can anyone explain it really well or point me to a good tutorial or example?
HABTM means has and belongs to many. You basically need a table as a middle man to track multiple id's (called a through table). When referenced as a typical users HABTM roles relationship, they really mean there would be a User model, Role model, users table, roles table, and a roles_users table. Don't forget to add the the HABTM -- roles_users -- table. A typical setup follows:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
You can then use the associations like normal saying User.first.roles and Role.first.users.
There are also a couple Railscasts on your issues.
The Ruby on Rails Guides are a good starting point here also this tutorial is exactly what you want
How do polymorphic associations work in Rails? What are their advantages? Is there a way to add belongs_to method just by running a migration?
Ryan has a railscast about this that is pretty good.
Belongs_to isn't something you add to a migration, you add it to the model. In the migration, you have to add the foreign key column. For example if you have a post model that belongs to a user, you'd add the user_id column to the post activerecord in a migration. Then you add
belongs_to :user
in the post model separately. Then rails will do its magic in the background to give you the proxy collections in the user model.
Say I have two tables, a master list of students containing personal info, and a list of student enrollments in classes. The two tables share a common column, which is a string uniquely identifying the student, but it is not the primary key.
Say I want to display all the enrollments on a page, along with some of the personal data from the student (say perhaps hometown).
I understand that it would be a has_many relationship. The master list record has many enrollments. An enrollment belongs to a student.
class Student < ActiveRecord::Base
has_many :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :student
end
Is this the correct relationship between the two, and if so, how do I do a join query against the shared column?
Yes ActiveRecord will manage the relationships for you, but you can also specify the join when searching for a condition in the relationship. For example:
User.find(:all, :joins => :phone_numbers, :conditions => { :phone_numbers => {:name => 'business'} })
Note though using a hash for the conditional declaration is only in Rails 2.2
But most of the time just using the ActiveRecord relationships with has_many should be just fine and as mentioned in above answers you would call the object using as if it was directly on the model... #student.enrollments
Ideally, Rails is expecting the following columns:
Student table:
- id
Enrollment table:
- student_id
In doing so, your relationship should work.
Then to try it out, drop into the Rails console and play around:
#student = Student.first
#student.enrollments
Take a look at this great Rails reference on using associations:
http://guides.rubyonrails.org/association_basics.html
What mwilliams says plus make sure you want a many to many association when you follow the guide.
The joined query is done automatically by association through ActiveRecord.