Rails updating multiple models on a single form - ruby-on-rails

Im writing a form which uses formtastic to manage the BusinessUnit model, however when creating a new BusinessUnit it also has to create a number of other record types. The associations between the models are as below:
class BusinessUnit < ActiveRecord::Base
has_many :business_unit_sites
has_many :locations
class BusinessUnitSite < ActiveRecord::Base
belongs_to :site
belongs_to :business_unit
class Site < ActiveRecord::Base
has_many :locations
has_many :business_unit_sites
class Location < ActiveRecord::Base
belongs_to :business_unit
belongs_to :site
When a BusinessUnit is created, a Site must also be created using BusinessUnitSite as a join table. In addition a Location record should be created which must hold a foreign key to the new Site record and this is where Im having problems.
I can create a new Location using a nested form (below) but the Site will have to be created manually.
<%= semantic_form_for #business_unit do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :business_unit_id %>
<%= f.input :business_unit_group, :include_blank => false %>
<%= f.input :business_unit_type %>
<%= f.input :tax_region, :include_blank => false %>
<%= f.semantic_fields_for :locations do |l| %>
<%= l.input :name, :label => "Location Name" %>
<% end %>
<% end %>
<%= f.buttons %>
<% end %>
What is the best way to create the Location, Site records and ensure that Location holds the foreign key of the newly created Site?

You probably want to do something like using the "fields_for" approach for the sub-objects in your form.
See this related answer:
Multiple objects in a Rails form
More info about fields_for:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

Related

Simple Form check box for join table relationship

I can't figure this out for the life of me but here are my models:
class User < ApplicationRecord
has_many :user_stores
has_many :stores, through: :user_stores
end
class UserStore < ApplicationRecord
belongs_to :user
belongs_to :store
end
class Store < ApplicationRecord
has_many :user_stores
has_many :users, through: :user_stores
end
So I have a join table, I'm trying to make a form, which would have selected checkboxes next to the store names that the user has selected (this information would come from the join table relationship) and open checkboxes for the remaining stores (coming from the Store model). How do I show that in the view/make it work in the controller as well. Would I use collections instead? ( I am using Devise and Simple Form gem )
This is what I have so far:
<h1>Add Favorite Stores</h1>
<%= simple_form_for(#user, html: { class: 'form-horizontal' }) do |f| %>
<%= f.fields_for :stores, #user.stores do |s| %>
# not sure if this is the right way or not
<% end %>
<%= f.button :submit %>
<% end %>
Store Controller:
class StoresController < ApplicationController
...
def new
#user = current_user
#stores = Store.all
# #user.stores => shows user's stores (from join table)
end
end
When you set up a one or many to many relationship in rails the model gets a _ids setter:
User.find(1).store_ids = [1,2,3]
This would for example setup a relation between user 1 and the stores with ids 1,2 and 3.
The built in Rails collection form helpers make use of this:
<%= form_for(#user) do |f| %>
<% f.collection_check_boxes(:store_ids, Store.all, :id, :name) %>
<% end %>
This creates a list of checkboxes for each store - if an association exists it will already be checked. Note that we are not using fields_for since it is not a nested input.
SimpleForm has association helpers which add even more sugar.
<h1>Add Favorite Stores</h1>
<%= simple_form_for(#user, html: { class: 'form-horizontal' }) do |f| %>
<%= f.association :stores, as: :check_boxes %>
<%= f.button :submit %>
<% end %>

Reading value with association (simple_form)

I have such models:
class Grade < ActiveRecord::Base
has_many :question_grades
end
class QuestionGrade < ActiveRecord::Base
belongs_to :grade
belongs_to :question
# it has integer :number
end
class Question < ActiveRecord::Base
# it has string :label
end
I have a simple_form for the 'grade' model, which iterates question_grades:
<%= simple_form_for #grade, :url => "/homeworks/update_grade", :method => :post do |f| %>
<%= f.simple_fields_for :question_grades do |q| %>
<%= q.association :question %>
<%= q.input :number, :collection => 0..2, label: false%>
</div>
</div>
This form creates an editable form for each 'question_grade', where allows visitors to edit 'number' attribute of question_grade. I also want to show a label by using the value, 'question_grade.question.label'. I created an association with 'q.association :question' but it creates an editable input form item. I want to access a value in the association. How can I do that?
When you do
<%= q.association :question %>
you are creating a field to edit this association, as you can see.
What do you need, is to access the q.object, defined as attr_reader here .
in this case, it will be your QuestionGrade instance.
so this:
<%= q.object.question.label %>
may solve your problem.

simple-form grouped collection select for has many through association

I have three models as shown
class Location < ActiveRecord::Base
attr_accessible :location_name
has_many :areas
has_many :restaurants, through: :areas
end
class Area < ActiveRecord::Base
attr_accessible :area_name, :location_id
belongs_to :location
has_many :restaurants
end
class Restaurant < ActiveRecord::Base
attr_accessible :description, :menu, :restaurant_name, :area_id
belongs_to :area
end
Am using simple-form gem and i want create a new restaurant and select a Location first which has many areas and the correct areas associated with a location to be automatically selected. Then i narrow down to a single area. Similar in concept to say how someone would select Continents and then be narrowed down to a country in a particular continent. Is there a way to achieve this using simple_form.?
Do i have anything extra to the new action in the restaurant controller?
This is my view so far for creating a new restaurant
<%= simple_form_for #restaurant do |f| %>
<%= f.input :restaurant_name %>
<%= f.input :description %>
<%= f.input :menu %>
<%= f.input :area_id,collection: #locations, as: :grouped_select, group_method: :areas%>
<%= f.button :submit %>
<% end %>
This doesnot work as expected. I have already populated my database with Locations and Areas. Any ideas?
You need to pass the options the other way around, collection is the parent group, not the child group. In your case you need:
<%= f.input :area_id,collection: #areas, as: :grouped_select, group_method: :locations %>

collection_select not filtering (in nested_form w/nested resources)

I've got an application that uses nested resources (see routes.rb below) to completely segregate users. It works great until I use collection_select to allow users to select objects from other models. For example, if I visit the store index view as user A, I only see stores created by user A. However, if I visit the store_group view and try to select a store to add to the group from the collection_select menu under the fields_for :store_group_details, I see all stores created by all users.
As far as I can tell, the problem might happen because there is no filter for stores in the store_group controller. store_group_details doesn't have a controller, but from what I've read, that seems to be correct since the model can only be accessed through a nested form in the store_group view. I have another situation where another view for another resource has several collection_select menus for selecting objects from other models, and all of those have the same problem (they display all objects in that model, regardless of which user created them).
How can I filter the objects shown in the collection_select menus? Is it a problem with what I'm passing into collection_select, or is it because the controllers don't do anything to filter the other models before those models' objects are displayed? I've looked at the docs for collection_select, and couldn't make it work based on that.
Thanks for your help, I've spent quite a bit of time trying to get this to work.
user.rb
class User < ActiveRecord::Base
has_many :store_groups
has_many :stores
has_many :store_group_details
end
store.rb
class Store < ActiveRecord::Base
belongs_to :user
has_many :store_group_details
has_many :store_groups, :through => :store_group_details
end
store_group.rb
class StoreGroup < ActiveRecord::Base
belongs_to :user
has_many :store_group_details, :inverse_of => :store_group
has_many :stores, :through => :store_group_details
accepts_nested_attributes_for :store_group_details
attr_accessible :store_group_details_attributes
end
store_group_detail.rb
class StoreGroupDetail < ActiveRecord::Base
belongs_to :store
belongs_to :store_group
belongs_to :user
attr_accessible :store_id
delegate :store_name, :to => :store
end
_store_group_form.html.erb
<div class="container">
<div class="span8">
<%= nested_form_for([#user, #store_group]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label "Store Group Name (required)" %>
<%= f.text_field :store_group_name %>
<%= f.label "Store Group Description" %>
<%= f.text_area :store_group_description %>
<%= f.fields_for :store_group_details %>
<p><%= f.link_to_add "Add store to group", :store_group_details %></p>
<br>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
_store_group_detail_fields.html.erb
<p>
<%= f.label "Select Store:" %>
<%= f.collection_select :store_id, Store.order(:store_name),
:id, :store_name, include_blank: true %>
<%= f.link_to_remove "remove" %>
</p>
routes.rb
resources :users do
resources :stores
resources :store_groups
resources :store_group_details
end
There must be a problem with your controller. Did you ever solve this issue?
Look at this answer as it might lead to a solution. Or add you stores_controller.rb.

Rails form with three models and namespace

Banging my head against this one for a long time. On Rails 2.3.2, Ruby 1.9.1.
Trying to use one form to create three objects that have these relations:
class Person
has_one :goat
end
class Goat
belongs_to :person
has_many :kids
end
class Goat::Kid
belongs_to :goat
end
Here's a summary of the schema:
Person
first_name
last_name
Goat
name
color
Goat::Kid
nickname
age
I'd like my #create action to instantiate new instances of all three models with the specified associations. However, while it appears that my params hash is being passed to the controller as it should (based on the backtrace logs in the browser when it blows up), the Goat::Kid object is not collecting the params.
irb (irb session is just a psuedo-representation of what I'm trying to accomplish so if it doesn't call #save! or any other necessities it's not really meant to be correct. I'm trying to do this all through the browser/web form.)
a = Person.new :first_name => 'Leopold', :last_name => 'Bloom'
b = Goat.new :name => 'Billy', :color => 'white'
c = Goat::Kid.new :nickname => 'Jr.', :age => 2
a.goat.kids
>> []
Now, I cannot figure out how to get the view to pass the params to each object and to get the controller to save these params to the db.
My questions: A) is this a good place to use nested_attributes_for and if so how do I declare that with a namespace? B) is there a much simpler, easier to understand way to do this?
Passing params to three models has just been very challenging to me and no matter how much documentation I read I can't wrap my head around it (#form_for and #fields_for). The namespace further complexifies this. Thanks for any help!
Addendum: if I end up declaring
accepts_nested_attributes_for
what's the proper way to use the symbol argument for a namespaced model?
accepts_nested_attributes_for :kids, :through => :goats
or
accepts_nested_attributes_for :goats_kids, :through => :goats
or
accepts_nested_attributes_for :goats::kids, :through => :goats
I'm not sure how namespaced models translate to their symbol identifiers. Thanks!
Well, this is my first time playing with accepts_nested_attributes_for, but with a little playing around I was able to get something to work.
First the model setup:
class Person < ActiveRecord::Base
has_one :goat
accepts_nested_attributes_for :goat
end
class Goat < ActiveRecord::Base
belongs_to :person
has_many :kids
accepts_nested_attributes_for :kids
end
class Goat::Kid < ActiveRecord::Base
belongs_to :goat
end
With a simple restful controller:
ActionController::Routing::Routes.draw do |map|
map.resources :farm
end
class FarmController < ApplicationController
def new
end
def create
person = Person.new params[:person]
person.save
render :text => person.inspect
end
end
Then comes the semi-complex form:
Next, the form setup:
<% form_for :person, :url => farm_index_path do |p| %>
<%= p.label :first_name %>: <%= p.text_field :first_name %><br />
<%= p.label :last_name %>: <%= p.text_field :last_name %><br />
<% p.fields_for :goat_attributes do |g| %>
<%= g.label :name %>: <%= g.text_field :name %><br />
<%= g.label :color %>: <%= g.text_field :color %><br />
<% g.fields_for 'kids_attributes[]', Goat::Kid.new do |k| %>
<%= k.label :nickname %>: <%= k.text_field :nickname %><br />
<%= k.label :age %>: <%= k.text_field :age %><br />
<% end %>
<% end %>
<%= p.submit %>
<% end %>
From looking at the source for accepts_nested_attributes_for, it looks like it will create a method for you called #{attr_name}_attributes=, so I needed to setup my fields_for to reflect that (Rails 2.3.3). Next, getting the has_many :kids working with accepts_nested_attributes_for. The kids_attributes= method was looking for an array of objects, so I needed to specify the array association in the form manually and tell fields_for what type of model to use.
Hope this helps.

Resources