I have a form set up for a Recipes model that accepts_nested_attributes_for Ingredients. The form and saving works fine, however, I want to check for the existence of each Ingredient in the database. The form seems to be creating new records for ingredients, even if they already exist in the database.
Recipes and Ingredients are related using a 'has_many :through' relationship. Is there a clean way to make sure that ingredients are duplicated when the form is submitted, but the relationships are still established?
similar question might help you on this, although the solution is not that eligant
Rails nested form on many-to-many: how to prevent duplicates?
It is about how you are taking input from the user - your forms are.
If you have a multiple select input for ingredients in new/edit form of recipe and you show all the existing ingredients there, user can choose the existing ingredients and the POST/PUT from these forms will have the existing ingredients' ids as part of form data. This will not create new ingredients then.
Related
Does any one know how to make 2 none nested models work on a single form for rails 4? For example, I have user model that has one profile(model) and one car(model).
Both profile and car models are nested under user but car and profile has no relationship. I figure I can do a form_for user and use both field_for profile/car but is there a better way to do this?
In this situation I would create a form object. Check out point '3. Extract Form Objects' in the following post: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
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 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
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
Rails3,jQuery. What I'm trying to do: Create a new WorkoutPlan that HABTM Exercises. Then create a new Workout that belongs_to a WorkoutPlan. I want the Workout#edit page to build a form with fields_for new/edit WorkoutRoutines, one routine for each exercise in the WorkoutPlan.
That last part is where I'm struggling, creating one Routine for each Exercise in the WorkoutPlan. Everything else works, its just this form build that sucks. Not sure if I'm making this overly complicated, or if there was an easier way. Any ideas?
From what I understand, using nested attributes should solve the problem without many complications. From the link,
Nested attributes allow you to save
attributes on associated records
through the parent. By default nested
attribute updating is turned off, you
can enable it using the
accepts_nested_attributes_for class
method. When you enable nested
attributes an attribute writer is
defined on the model.
The attribute writer is named after
the association, which means that in
the following example, two new methods
are added to your model:
author_attributes=(attributes) and pages_attributes=(attributes).
anytime you need a complex form in rails its either going to be a pain, or can use formtastic. super easy awesome forms, plenty of ways to deal with habtm relations as well.