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
Related
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.
I implemented the Twitter-like blog in Michael Hartl's Ruby on Rails Tutorial. I now want to add my own features to further understand Rails. I have users following others through the Relationship model. Now I want to give users the ability to sort the people they are following by creating custom Category model - That is, I could create personal custom categories for friends, family, etc. and place the people I'm following in the correct group.
The way I have thought about implementing this is by creating a Category model and implementing the association through an intermediary model similar to Relationship such as CategoryList. Therefore, each Category will has_many Following through CategoryList. Is the most effective / Rails way to handle the issue?
You should have a model category that is simply the list of all of the categories. Then you should have a category_relationship join table that represents all of the entries. So a relationship has many categories through category_relationship. At least that is how I would do it.
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
So I just started with Rails and right now looking over HABTM. I am reading the DHH book and I see that he has two models namely article and user. They have HABTM relationships. I am wondering however whether I need to create a separate migration for the articles_users model by myself or will Rails do it for me?
If so, what happen if I create a new user and associate it with an article? Will Rails know right away what to enter inside the articles_users table?
Ex:
u = User.new(:name => "John");
a = Article.new(:title =>"Rails");
#can I do this?
a.user << u
#will rails automatically create an entry inside articles_users table?
I am somewhat confused on where Rails stop in terms of making tables for us or whatnot.
You will need to manually create the table with a migration. However, most rails developers now prefer Has Many Through instead of HABTM. Another benefit is that when you generate the "join model" rails will make a migration for you!
I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.