Is such association possible in rails? - ruby-on-rails

I have a List model and a User model. A List belongs to a User and also can be followed by many Users. How do I make association so that I can do such operations:
list.user - gives me the user that created(belongs to) the list.
list.followers - gives me a list of users following the list.
user.following - gives me all the lists followed by a user.
can someone please help me create such associations in rails as well as how do I create migrations for this association.

First of all generate a user model.
Then for list model, give migration like:
$rails g model list user:references follower_id:integer followee_id:integer
then $rake db:migrate
Now in list.rb
validates_uniqueness_of :followee_id, scope::follower_id
Hope it helps
Edit
so for full code do refer my blog https://additionalknowledge.wordpress.com/2017/08/06/making-follow-mapping-on-rails/#more-559 follow each step don't miss.

Related

Cannot decide Model Associations in rails

I am new to rails and having a hard time figuring out what is the best association I can create for my models. The models I have are:
User
Article
List
ArticleLink
ArticleMeta
etc..
Here, User can create many Articles and Lists, an Article and List can belong to multiple Lists, an Article can have multiple ArticleLinks and ArticleMeta. I also want to delete all the references of a model wrt User when it is deleted. For example when an User deletes a List, all the reference of the List should be deleted from join tables.
Also please let me know what would be the most efficient way to insert records in each model and retrive records as well
Thank you for your help.
You can find all your answers over this link
http://guides.rubyonrails.org/command_line.html
As example
rails generate scaffold Articles name:string category:string lists:references
or
rails generate scaffold ArticleLinks name:string Article:references

Are associations necessary in both model and table?

I'm rather new to RoR and have been going through the associations guide provided at rubyonrails.org.
I've noticed though in each example, when it gives what "the corresponding migration might look like", they've included the association in the migration table.
I've been including associations in my models, is it necessary for me to add them within the migration tables to? Or is the guide providing this to help readers understand the association?
For the record, I do understand if an association is made between two objects, each object model needs to have an id to associate with.
Thanks in advance!
Kristian,
Associations can get a little confusing.
You only need to add them in your models. For example if I have a post model with a user.
Post.rb (model)
belongs_to :users
User.rb
has_many :posts
Here is a link that may help you
http://www.tutorialspoint.com/ruby-on-rails/rails-models.html
Here is a railscast video that might help:
http://railscasts.com/episodes/154-polymorphic-association?view=asciicast
PS: You do not need to add them to migrations. Migrations are there to rebuild the database if you need to restart or rollback

Ruby on Rails Model Relationships

I am very new to Ruby on Rails.
I am trying to set up a relationship between a user model and a model of ten different items.
My goal is to have users be able to check off items in the items model and then have the ones that have been checked off display on their profile.
I have used the Michael Hartl Ruby on Rails tutorial up to
the point of creating microposts.
Any tips on tutorials that will help me complete this would be greatly appreciated.
Thanks!
Basically, what you want is:
A User has_and_belongs_to_many :items
Also, an Item has_and_belongs_to_many :users
This is many to many relationship. Since, a user can has many items, and an item can belong to many users too. In rails, here has_and_belongs_to_many will implicitly create a table items_users which will contain id's of both, establishing the relationship.
Read more about this association here - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
Use checkbox tag for showing checkboxes for all the items. Documentation - http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
Based on whatever checkboxes are checked, save the records, establishing the relationship.
Done. :)
I don't know about other tutorials, if you've completed Hatel's then you have a very very good understanding of the rails framework as a whole. I would have an items_list model. Which had a user_id foreign key to associate itself with a user. Then I could have an items model which had an items_list foreign key to associate them to a list. Then items model could have a boolean field "active" or "checked" or whatever. Using these, and the associated relations, and some scopes, you can get what you want.
Just make sure to use the includes helper when you request this data, otherwise you'll easily get a N+1 problem.
http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

Modifying models to create an association, what is the right way to do this?

Say I have two models of an existing Rails 3 application that I want to create an association for:
User
AccountType
I want to create an association such that I can do:
User.account_type
AccountType.users
So a user has a single account_type, and a account type has 0 or many users.
At the db level, the user will have a account_type_id and the AccountType table will not have any association related columns for User.
So my first step would be to write a test right? So in both of my user_spec.rb and account_type_spec.rb I should create a simple test to see if it has the proxy class .account_type an account_type.users exist right? anything else?
Modify the User.rb model and add belongs_to :AccountType, and in AccountType.rb add a has_many right?
Create a migration script, do I just add account_type_id or do I use a special way to reference the AccountType?
Regarding testing: i'd agree with Dave Newton's comment that you should test whatever behaviour you intend to create with this association (so write tests for the ability to create an association, to look up a user or account by this association, etc) but that testing for the existence of the methods as you describe is maybe going farther than necessary.
Your steps 2 and 3 look right to me.
rails g migration add_account_type_id_to_users account_type_id:integer
should generate the migration you need.

Rails : Model with Array of object of my class

I'm beginning RoR. I've designed my model like this :
User
-login:string
-password:string
-email:string
-followers:array (type user)
I have now this termainal rails command :*
rails generate model User login:string password:string email:string
but i don't know how to tell my generated model that I would like an array of User.
I think my question is a bit stupid because Ruby is like PHP (no type). But I prefer to ask ...
Thanks for your help !
If you want to have something like followers you have to use a many-to-many association.
Take a look at associations in the rails guide : http://guides.rubyonrails.org/association_basics.html
You have to remember that when you generate a model and you specify login:string for example you specify the name and the type of the column which will be created in your database.
The right way is to have a many-to-many relationship. You have to say that your User has_and_belongs_to_many followers (I assume if a Use has many followers he can follow many User?). You will need to create an other table which will associate a user to another.
You'll find many articles on google which explain how to create many to many relationship.
But RailsGuides are really well done, take a look at it first.
Edit:
As your follower will be also be of type User, you'll have to do something like that:
has_and_belongs_to_many :followers, :class_name => "User"
You can take a look at the documentation for other options:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

Resources