I have two models:
User
class User < ActiveRecord::Base
has_and_belongs_to_many :partners
end
and Partner
class Partner < ActiveRecord::Base
has_and_belongs_to_many :users
end
and now, i want change it to:
class User < ActiveRecord::Base
has_many :partners
end
class Partner < ActiveRecord::Base
belongs_to :user
end
but how can i do it by migration?
You can change the model's to:
class User < ActiveRecord::Base
has_many :partners
end
class Partner < ActiveRecord::Base
belongs_to :user
end
And Partner model should have a user_id column
If you don't have a user_id column in Partner model, you can add it by:
rails g migration add_user_id_to_partner user_id:integer
The intermediate table partners_users should be dropped since it is a has_many Association.
To drop the table create an empty migration and then add this to the migration file.
drop_table :partners_users
Then run the migration using rake db:migrate
Related
What is the correct way to define one to many association in rails 5.we
have currently the following models.we have to define
products and categories tables such that one category has many products.
class Category < ApplicationRecord
has_many :products, dependent: :destroy
end
class Product < ApplicationRecord
belongs_to :category, index: true
end
Error:AssociationtypeMismatch
First, generate your models as so:
rails generate model Category title:string
rails generate model Product title:string category_id:integer
this will create the two models with their migration files...
rake db:migrate # run migrations
and in app/models
# app/models/category.rb
class Category < ApplicationRecord
has_many :products, dependent: :destroy
end
# app/models/product.rb
class Product < ApplicationRecord
belongs_to :category
end
I have User, Teacher and ClassRomm model using STI as following
class User < ApplicationRecord
end
class Teacher < User
has_many :class_rooms
end
class Student < User
has_many_and_belongs_to :class_rooms
end
class ClassRoom < ApplicationRecord
belongs_to :teachers
has_many_and_belongs_to :students
end
my question how can i create migration for all relationships between user,teacher,Student and classRooms ?
for example should class_rooms has forignKey column for instructor_id or user_id
first of all
in ClassRoom association should be singular belongs_to :teacher, without s
also ClassRoom mast have column teacher_id, than create "third" table with name ClassRoomsStudents for has_many_and_belongs_to association
and it should works
Rails newbie here; so I have three classes - user, article, comment - where,
class User < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
Now I'm wanting the comments to be user specific i.e. each comment will be linked to a user (just how every article is linked to a user). How do I go about doing that apart from adding has_many and belongs_to in user.rb & comment.rb? I hope I made myself clear.
Add similar relations to User and Comment. So a comment will have an user_id as well as an article_id.
class User < ActiveRecord::Base
has_many :articles
has_many :comments
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
You will have to write a migration after alerting your models to add an user_id field to your comments table.
rails g migration AddUserRefToComments user:references
Then run rake db:migrate. Hope that helps.
I have three models : User, Agency and Client.
Currently,
class User < ActiveRecord::Base
has_one :agency
has_one :client
end
class Client < ActiveRecord::Base
belongs_to :users
end
class Agency < ActiveRecord::Base
belongs_to :users
end
I want to change associations and create a polymorphic association such as this:
User belongs_to :role , :polymorphic => true
and
Client has_one :user, as: :role
Agency has_one :user, as: :role
I am a novice rails developer. How can I achieve this? HOw to write a migration?
You need to add two fields, role_id and role_type in user model. You can create new migration as follows
rails g migration addNewFieldsToUsers role_id:integer role_type:string
After running rake db:migrate you need to modify the associations as follows
class User < ActiveRecord::Base
belongs_to :role, polymorphic: true
end
class Client < ActiveRecord::Base
has_one :user, as: :role, class_name: 'User'
end
class Agency < ActiveRecord::Base
has_one :user, as: :role, class_name: 'User'
end
Now restart the rails server.
A migration is not needed. There are no associations between models in the database (which is what a migration would change).
What you need to do is change the models app/models/user.rb and app/models/location.rb. simply remove the belongs_to: from user and add it in loction: belongs_to: user.
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
end
class User < ActiveRecord::Base
has_many :posts
end
class Category < ActiveRecord::Base
has_many :posts
end
I want to get all the posts that have a relation to user AND category. Is any similar possible:
user.category.posts
Or do I need to do:
user.posts.where(category_id: category.id)
You have 1-M relationship between User and Post.
In User model association should be has_many :posts (note plural)and not has_many :post(singular).
Update your model User as below:
class User < ActiveRecord::Base
has_many :posts ## Plural posts
end
To answer below question:
get all the posts that have a relation to user AND category
Assuming that you have local variables user and category as an instance of User model and Category model respectively.
For example:
user = User.find(1); ## Get user with id 1
category = Category.find(1); ## Get category with id 1
user.posts.where(category_id: category.id) ## would get you what you need.
Also, user.category.posts will not work as User and Category models are not associated.
Try:-
user.posts.where(category_id: category.id)
Change your association like
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
end
class User < ActiveRecord::Base
has_many :post
belongs_to :category or has_one :category
end
class Category < ActiveRecord::Base
has_one :user or belongs_to :user
has_many :posts
end
in users table add column category_id,or add column user_id in categories,because you don't have relation between those two tables.if you don't have relation you can't use association API's.then you have to use manual API to fetch the data.like how you have mentioned your question.