User created categories for followed users in Rails - ruby-on-rails

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.

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

Special Case Models in ActiveRecord Schema (Rails)

I have a project that is built with a tagging model to reference three different models: artist, article, event. I have associated each model to the tagging model via has_many through:. I have two problems; both related to each other:
I have 5 "default" methods that I wish to be able to call restfully from the tagging controller/model: popular, upcoming, events, articles, and artists. Each method is designed to do exactly as it's name implies. The issue lies within these methods being a query by nature. How can I maintain a consistent schema where 5 of the many taggings models I have require a special attribute (let's call it content) that should subsequently call the appropriate method?
When calling any one of the taggings models, it will return (in it's content attribute) an array of the 3 models specified earlier. Other than adding a method within each model that contains a localized description of the model and then calling upon that type method to match another hardcoded string elsewhere, is there any alternative? I don't like how the implementation I just described requires me to hardcode values. Eek.
I'm very new to Ruby on Rails, so I apologize if this is an obvious solution. However I have spent a week looking into ways to solve this compound problem that I'm trying to solve. Any input is appreciated!
As far as i unterstand you need a polymorphic association. Because tags can be applied on different models, the clue is to treat all these models in polymorphic manner by marking them as :taggable on the association to tags.
I would just use the popular acts_as_taggable_on gem for it.

Model relationship in rails and their handling

I have a requirement for an app where two models invoice and message needs to be linked. The link/relationship should be able to do following things:
The invoice should be able to store the message_id.
The message should also be able to store the invoice_id - a foreign key to the invoice table.
There are some extra fields in both models.
Also can you tell me how will i be able to generate a form_for for this kind of models, where two models get data at the same time but of different fields. Should i use hidden_fields?
Please Help.
Here You will learn about has_one and belong_to associations, which can be used with table structure You are searching for (though without extra explanation it sounds odd).
http://guides.rubyonrails.org/association_basics.html#the-belongs-to-association
http://guides.rubyonrails.org/association_basics.html#the-has-one-association
After linking You models check nested attributes Railscasts to get and idea how You can construct form for multiple elements with mass-assignment supported:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

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

Routing Users to single Models with Rails

I'm creating a Rails app for students and high schools and I'm having some trouble with my User.rb.
I want to have a user model to be used for logging in, but having that user have many roles. The tricky part is that I want users that have a student role to have_one student page, and those that have a role of principal to have_one high_school page.
The students and also nested in the high_school so the entire thing becomes a big mess.
So my question(s): How do I limit a user to only creating one student / high school to represent them? Also how would I nest this student pages inside the highschool without screwing up the user system?
My environment: Rails3 and Ruby 1.9.2dev
Thank you!
Follow up: Would it be possible to put the name of the high_school in the subdomain? That would make the url look like
highschoolname.mysite.com/students/eric-koslow
I'd suggest polymorphic association to user_representations. It'd hold info about which high_school object or which student_page to associate the appropriate user to.
You can made a validation to avoid the multi-creation.

Resources