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
Related
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 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
I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly
I am building a blog-style application in Rails 3 where multiple users are able to post some news. After the login (which is realized with "Authlogic") the user values are stored in a own model called e.g. "UserSession". The form for the post contains title, content etc. and the username should be stored with a hidden form.
I think that the two models don't need to be related to each other (by that I mean a :has_many - :belongs_to relationship) because there isn't any further usage for that information.
Do I really not need this relation? And how could I realize the form?
For Authlogic is it important to remember that the 'UserSession' does not correspond to any database tables (i.e. you would never use a has_many or has_one 'UserSession'). I think the relationship you are looking for is:
User has many Posts
Blog belongs to User
The reason? It is always a good idea to associate a record with the 'owner' so that the owner can later modify or delete the record. I hope this helps.
I have two models Users and Roles. I have setup a many to many relationship between the two models and I have a joint table called roles_users.
I have a form on a page with a list of roles which the user checks a checkbox and it posts to the controller which then updates the roles_users table.
At the moment in my update method I am doing this because I am not sure of a better way:
role_ids = params[:role_ids]
user.roles.clear
role_ids.each do |role|
user.roles << Role.find(role)
end unless role_ids.nil?
So I am clearing all the entries out then looping threw all the role ids sent from the form via post, I also noticed that if all the checkboxes are checked and the form posted it keeps adding duplicate records, could anyone give some advice on a more efficent way of doing this?
You can do a direct assignment, as that handles the dirty work for you:
user.roles = params[:role_ids].present? ? Role.find_all_by_id(params[:role_ids]) : [ ]
ActiveRecord should take care of creating new associations or removing those that are no longer listed. If anything precludes your join model from saving, such as a failed validation, you may have issues, but in most situations this should work as expected.
I hope you're using a has_many ..., :through for this one and not the deprecated has_and_belongs_to_many that keeps haunting so many Rails apps because of old example code.