How to associate a user in a Rails nested model form? - ruby-on-rails

Given the typical nested model example from Railscasts:
http://railscasts.com/episodes/196-nested-model-form-part-1
How would you associate the answers to a user?
Rephrased, if you're building a model through another model, how would you associate a user for every child element created?
Rephrased yet again with another example:
If I had Albums and Pictures and I created many Pictures at once through the Album's controller associating it to that Album, how would I associate every Picture in that same update_attributes call or whatever the most idiomatic way for Rails is?

I ended up using the following to make it work:
params[:exercise][:log_entries_attributes].each do |value|
value[1].merge!(:user_id => current_user.id)
end
Is there a more Rails or Ruby appropriate version of handling this?

Related

Ruby on Rails adding relationship doesn't update new form

Basic rails question.
I have a class, let's say User, and another class, Category.
I set up a relationship between them, so that Users have categories and categories have users. When I go to users/new to create a new user though, there is no prompt for category. I'm expecting the rails magic to kick in and have done that, so I'm wondering if my expectations were wrong or if I haven't executed something properly.
You should set your User model to belong_to :category, and the Category to have_many :users.
That way, when you want to create a new user, it will ask you what category the user belongs to. You will probably need to run a migration to add the category_id index to the users table though. That will solve your problem.

Submitting two tables/model data with single/two form on one webpage with single submit button?

My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

2 none nested model in 1 form rails

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/

In a single form, linking two instances of two models with habtm

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

Multiple non-related models in one Rails form

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.

Resources