How to create multiple "has_many through" associations through one form? - ruby-on-rails

I'm building a martial arts related database, currently I have the following associations set up:
Student has_and_belongs_to_many :styles
Style has_many :ranks
Student has_many :ranks, through: :gradings (and vice versa)
I'm generating a form as follows, depending on the student's styles:
So the headings are generated by the Style model (Tai Chi, Karate...), then their rankings listed below (taken from the Rank model), and the "Dojo" and "Date" fields should belong to the Grading model once created.
The question: I know how to build a form that creates one association (or one association + its children), but how do I build a form that creates multiple associations at once?
Also, what would be a clean way to implement the following:
Only lines which are ticked become associations
Dojo and date must be filled in for ticked lines to save successfully
If a line is unticked it will destroy any previously created associations
This is what I've currently implemented to retrieve the correct records:
class GradingsController < ApplicationController
before_filter :authenticate_sensei!
def index
#student = Student.includes(:styles).find(params[:student_id])
#ranks = Rank.for_student_styles(#student)
split_ranks_by_style
end
private
def split_ranks_by_style
#karate = #ranks.select_style("Karate")
#tai_chi = #ranks.select_style("Tai Chi")
#weaponry = #ranks.select_style("Weaponry")
end
end
# Rank model
def self.for_student_styles(student)
includes(:style).where("styles.id in (?)", student.styles.map(&:id))
end
def self.select_style(style)
all.map { |r| r if r.style.name == style }.compact
end

Complicated forms like this are best handled in a service object initiated in the primary resource's create or update action. This allows you to easily find where the logic is happening afterwards. In this case it looks like you can kick off your service object in your GradingsController. I also prefer formatting a lot of the data in the markup, to make the handling easier in the service object. This can be done a'la rails, by passing a name like "grade[style]" and "grade[rank]". This will format your params coming in as a convenient hash: {grade: {style: "karate", rank: "3"}}. That hash can be passed to your service object to be parsed through.
Without really grasping the full extent of your specific requirements, let's put together an example form:
<%= form_for :grading, url: gradings_path do |f| %>
<h1><%= #rank.name %></h1>
<%- #grades.each do |grade| %>
<div>
<%= hidden_field_tag "grade[#{grade.id}][id]", grade.id %>
<%= check_box_tag "grade[#{grade.id}][active]" %>
...
<%= text_field_tag "grade[#{grade.id}][date]" %>
</div>
<%- end %>
<%= submit_tag %>
<%- end %>
With a form like this, you get your params coming into the controller looking something like this:
"grade"=>{
"1"=>{"id"=>"1", "active"=>"1", "date"=>"2013-06-21"},
"3"=>{"id"=>"3", "date"=>"2013-07-01"}
}
Nicely formatted for us to hand off to our service object. Keeping our controller nice and clean:
class GradingsController < ApplicationController
def index
# ...
end
def create
builder = GradeBuilder.new(current_user, params['grade'])
if builder.run
redirect_to gradings_path
else
flash[:error] = 'Something went wrong!' # maybe even builder.error_message
render :action => :index
end
end
end
So now we just need to put any custom logic into our builder, I'd probably recommend just making a simple ruby class in your /lib directory. It could look something like this:
class GradeBuilder
attr_reader :data, :user
def self.initialize(user, params={})
#user = user
#data = params.values.select{|param| param['active'].present? }
end
def run
grades = data.each{|entry| build_grade(entry)}
return false if grades.empty?
end
private
def build_grade(entry)
grade = Grade.find(entry['id'])
rank = grade.rankings.create(student_id: user, date: entry['date'])
end
end
There will obviously need a lot more work to pass all the specific data you need from the form, and extra logic in the GradeBuilder to handle edge cases, but this will give you a framework to handle this problem in a maintainable and extensible way.

Related

I want to use one controller and html.erb files for my dynamic table. How I will do it in Ruby On Rails?

I stored all the tablename I've created to Menu table. And every time I add the table in Menu, it will automatically create a link under Menu list
see below.
I want each table in Menu to have a Listing, New, Edit, and Delete.
see below.
I have a controller prj_menus_controller, I will just pass the id of the table from Menu table.
here is the code for index and new in my controller.
Class PrjMenusController < ApplicationController
def index
#prj_menus = Menu.find(params[:id]).tablename.singularize.classify.constantize.all
end
def new
#prj_menu = Menu.find(params[:id]).tablename.singularize.classify.constantize.new
end
def create
#prj_menu = Menu.find(params[:id]).tablename.singularize.classify.constantize.new(prj_menu_params)
if #prj_menu.save
redirect_to :action => 'index'
else
render :new
end
end
private
def prj_menu_params
params.require("HERE IS MY PROBLEM").permit(:name)
end
end
and in my
new.html.erb
<%= simple_form_for (#prj_menu),:url => prj_menus_path, :method => :post do |f| %>
<%= f.input :name %>
<%= f.submit 'Save', class: 'btn btn-primary' %>
<%= link_to "Cancel", :back, {:class=>"btn btn-default"} %>
<% end %>
I can get the list in my index.html.erb, it is working. My problem is that I don't know how to get all params when I click the submit in new.html.erb. I got this hash
{"sample1_table"=>{"name"=>"test 6"}, "commit"=>"Save","controller"=>"prj_menus", "action"=>"create"}
It is correct but I don't know what to put in my controller. I tried this params.require(["#{#prj_menu}"]).permit(:name), it creates new record but params[:name] does not save.
I am still a noob to Ruby On Rails and I don't know what to search for this.
I think you are mostly confused on what parameter whitelisting does and how parameters are passed from the form to the controller.
I does not really matter if the name of the form hash matches the name of the database table. It just does in most cases since that makes the most sense. It's simply representative of the REST interface of your app.
Let's say you have a action which creates Pets:
POST /pets
And in our form we have a bunch of inputs like so:
<input name="pet[name]">
Rails will map create a params[:pet] hash { name: 'Spot' }. But we want to save the pets as an Animal.
class PetsController < ApplicationController
def new
#pet = Animal.new()
end
def create
#pet = Animal.new(pet_params)
if #pet.save
# ...
end
def pet_params
params.require(:pet).permit(:name)
end
end
Animal does not care what the params key is, it just gets a hash. But we also need to tell simple_form what parameter key we want to use since it looks at the model_name attribute.
simple_form_for(#pet, as: :pet)
Gives us pet[name] instead of animal[name].
I don't get why you are so adamant about making things so difficult for yourself though unless you are creating a database administration tool in the vein of PHP_MyAdmin. And even that case you don't even want to be altering the schema of the app database at runtime.
You are going to run into huge problems when it comes to creating effective queries for getting all the menus.

Display multiple model results in your views in rails app

I'm adding a new model to my equasion and I'm wondering if there is a way to associate two models into one model then display any/all results within a view. For example, here is what I've currently have;
#tweet_category.order("position").each do |tweet|
<%= tweet.title %>
end
just a short example... now what if I added facebook into this. I was first thinking of creating a model thats named stuff then associate it to tweet_category and facebook_category like so;
class Stuff < ActiveRecord::Base
attr_accessible :title
belongs_to :user
has_many :tweet_category
has_many :facebook_category
end
Now in my controller I'm guessing I would do the following;
class StuffController < ApplicationController
def index
#stuff_list = Stuff.find(:all)
end
end
and in my view I would just simply do the following from above view;
#stuff_list.order("position").each do |stuff|
<%= stuff.title %>
end
am I understanding the logic here??? would that work having two models / two tables db.. etc..
First of all, I don't understand why you would need that "stuff" model. It belongs to users and has_many tweet_category and facebook_category, and just does nothing but offering a "title", when your User model could do the job ( I mean, each user could have many tweets and fb category, instead of having one or several "stuff" which has/have many of them ).
Anyway, if you want to make links between your models and then display everything in a view, first in your User model you just have to do :
class User < ActiveRecord::Base
...
has_many :facebook_categories #( I don't know how rails would pluralize it, btw, I'm just making an assumption )
has_many :tweeter_categories
end
and
class Facebook_category
...
belongs_to :user
end
and do the same fot the tweeter category
Then in your controller :
def show_everything #Here it's a custom action, but you can call it wherever you want
#users = User.all
end
And finally in your view :
<% #users.each do |user| %>
<% user.facebook_categories.all.each do |fb_c| %>
<%= fb_c.title %>
<% end %>
<% user.tweeter_categories.all.each do |t_c| %>
<%= t_c.title %>
<% end %>
<% end %>
Maybe just try to grab a better name for your models, so the pluralization doesn't get messy ( and I saw that the ".all" method is deprecated, so maybe replace it with something
Hope it helps !
Edit :
Basically, when you're doing
#users = User.all
What rails' doing is putting every hash defining every "User" in an array. So, if you want to mix two tables' arrays inside a single array, you can do something like this :
#categories = [] << Facebook_category.all, Tweeter_category.all
You will then have an array ( #category ), filled with 2 arrays ( one ActiveRecord relation for Facebook_category and one for Tweeter_category ). Themselves filled with hashes of their model. So, what you need to do is :
#categories.flatten!
Here's the API for what flatten does ( basically removing all your nested arrays inside your first tarray )
Now, you got a single array of hashes, being the informations from both your model's instances. And, if these informations can be ordered, in your view, you just have to :
<% #categories.order("updated_at").each do |i| %>
<%= i.title %>
<% end %>

nested_form not saving to model Rails 3

I think I am on the right path for the following, though i cannot save the form data to the model. I have 2 models
class Prediction < ActiveRecord::Base
attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes
has_many :fixtures
accepts_nested_attributes_for :fixtures
end
class Fixture < ActiveRecord::Base
attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time
belongs_to :predictions
end
To create a new prediction record i have a form that takes all the fixtures and pre populates the form and the user will just add scores next to each team
<%= form_for #prediction do |f| %>
<!-- Gets all fixtures -->
<%= f.fields_for :fixtures, #fixtures<!-- grabs as a collection --> do |ff| %>
<%= ff.text_field :home_team %> VS <%= ff.text_field :away_team %><%= f.text_field :home_score %><%= f.text_field :away_score %><br>
<% end %>
<%= f.submit 'Submit Predictions' %>
<% end %>
Then i have my controller to take care of the new/create action, which i think is where i may be falling over
class PredictionsController < ApplicationController
def new
#prediction = Prediction.new
#prediction.fixtures.build
#fixtures = Fixture.all
end
def create
#prediction = Prediction.new(params[:prediction])
#prediction.save
if #prediction.save
redirect_to root_path, :notice => 'Predictions Submitted Successfully'
else
render 'new'
end
end
end
and finally my routes
resources :predictions
resources :fixtures
So when i submit the form i get the error
ActiveRecord::RecordNotFound in PredictionsController#create
Couldn't find Fixture with ID=84 for Prediction with ID=
Looking at the params being parsed (snapshot below), something does not look right, for one the home_score and away_score are not being passed through.
{"utf8"=>"✓",
"authenticity_token"=>"DfeEWlTde7deg48/2gji7zSHJ19MOGcMTxEsQEKdVsQ=",
"prediction"=>{"fixtures_attributes"=>{"0"=>{"home_team"=>"Arsenal",
"away_team"=>"Norwich",
"id"=>"84"},
"1"=>{"home_team"=>"Aston Villa",
"away_team"=>"Fulham",
"id"=>"85"},
"2"=>{"home_team"=>"Everton",
"away_team"=>"QPR",
"id"=>"86"}
Current Output of form
Any advice appreciated
Thanks
When you set #fixtures to Fixture.all in the new method in your prediction controller, you are including every fixture, not just the fixtures belonging to your prediction. When the results of the form are passed to the create controller there are fixtures associated with other predictions being passed in which is the source of the error you have reported. Perhaps you want something like #fixtures = #prediction.fixtures.
What you are doing in the fields_for block also looks fairly wrong to my eyes. You are using f.text_field for your home_score and away_score inputs. This will repeat the same form element for the prediction model in each fixture field. You won't get the result you want from this. To be honest, I'm struggling to understand how this association makes sense. Are you able to explain it a little better? My suspicion is that your models are not quite set up the way you need them to be.
edit:
OK, I think I have a better idea of what you're trying to achieve now. You're trying to create many predictions in one form and prefill the home_team and away_team from a list of existing fixtures, right?
Okay, assuming that is so, you're definitely approaching it the wrong way. You don't need a many-to-one relationship between your prediction and fixture models (as you have correctly surmised). What you need to do is generate your form by iterating over the collection of fixtures and populating the home_team and away_team fields from the current fixture instance. Do you actually need these to be editable text fields, or are you just putting them in a text field so they get passed through? If so, you could use hidden fields instead.
The problem now though, is that Rails doesn't easily allow creating multiple records in the one form. It's not something I've done before and it would take me quite a bit of trial-and-error to make it work, but here's a best guess for one way of making it so.
models
class Prediction < ActiveRecord::Base
attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes
end
class Fixture < ActiveRecord::Base
attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time
end
controller
class PredictionsController < ApplicationController
def new
#prediction = Prediction.new
#fixtures = Fixture.all
end
def create
begin
params[:predictions].each do |prediction|
Prediction.new(prediction).save!
end
redirect_to root_path, :notice => 'Predictions Submitted Successfully'
rescue
render 'new'
end
end
end
view
<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
<% #fixtures.each do |fixture| %>
<%= fixture.home_team %> vs <%= fixture.away_team %>
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
<%= text_field_tag "predictions[][home_score]" %>
<%= text_field_tag "predictions[][away_score]" %><br />
<% end %>
<%= submit_tag "Submit predictions" %>
<% end %>
So essentially I'm creating a form that returns an array of parameters. I can't use the model form helpers in this situation.
One way around this would be to create a dummy model that has_many predictions and create a nested form using this.
Anyway, that's a lot of untested code that may never get looked at so I'm going to leave it there for now.
I haven't spent a lot of time looking because my wife is making me leave soon. But if I understand things right each of your rows in your view is from fixtures and predictions model association. The params hash you are getting is a mess because default behavior is going to be a view that is updating a single record ID. So you are bringing multiple records into a single view and trying to update multiple records at once.
Your create method in your model
#prediction = Prediction.new(params[:prediction])
But your params hash being passed is this:
{"utf8"=>"✓",
"authenticity_token"=>"DfeEWlTde7deg48/2gji7zSHJ19MOGcMTxEsQEKdVsQ=",
"prediction"=>{"fixtures_attributes"=>{"0"=>{"home_team"=>"Arsenal",
"away_team"=>"Norwich",
"id"=>"84"},
"1"=>{"home_team"=>"Aston Villa",
"away_team"=>"Fulham",
"id"=>"85"},
"2"=>{"home_team"=>"Everton",
"away_team"=>"QPR",
"id"=>"86"}
So you have to get more logic in your create method in your predictions controller to iterate through the hash you are receiving and save each row one at a time.

How to accept comma-delimited list to build tags for model?

I'm switching off of acts_as_taggable_on because of limited flexibility for my current application. Instead, I'm building my tags from scratch as their own model. However, I already miss the "#model.tag_list" method for forms, that would split up comma-delimited user input and make individual tags. My "taggable" model is a video, and I'm curious how to write a method that can essentially act like the "tag_list"?
Example:
<%= form_for #video do %>
<%= f.text_field :tag_list %>
....
Given the input: "one, two, three"
Would build three Tags as children of the #video.
#video.tags.each do |tag|
puts tag.name
end
=> one
two
three
EDIT
I'd really like this in the Tag model, to keep form cluttering my controller. Maybe as a custom attribute? Maybe the Video model would make more sense? I know how to make a custom method to return custom data, but not assign it. Some research points me this way (not yet tested)
video.rb
def tag_list=value
value.split(',').each do |tag|
self.tags.build(:name => tag).save
end
end
It seems like my example code ended up working
video.rb
def tag_list=value
value.split(',').each do |tag|
self.tags.build(:name => tag).save
end
end
EDIT
Also need to add to get it to work in a form:
def tag_list
self.tags.join(',')
end
You can leave tag_list as an attribute on Video, then in your create you could just have something like:
def create
#video = Video.new(params[:video])
if #video.save
params[:video][:tag_list].split(',').each do |tag|
#video.tags.create_by_name(tag)
end
else
render :new
end
end

How to associate, disassociate objects using has_many

I'm still very new to rails programming, and it's my first question at stackoverflow.
I have models Group and Product like so:
class Group < ActiveRecord::Base
has_many :products
end
class Products < ActiveRecord::Base
belongs_to :Group
end
Now, I have some data filled with multiple products in multiple groups. What should I do when I want to move one of the product from one group to another?
-Should I be using has_many :through relationship (and is that the only suggested way)? (Because I think that dealing with 3 tables are more complicated and calculation-intensive than just 2 tables.)
-Is there a preferred way for doing this?
Here's the things I've tried:
-Setting nil to group_id in products and updating it to remove the association which worked. I'm just not sure if this is the right way of disassociating an object.
-I tried creating a custom function in groups_controller.rb like below. (and I know that this may not be the right way of doing it, but I'm a bit desperate)
in the /view/groups/show.html.erb I have
<% form_tag group_add_reference_path(#group.id), :method => 'put' do %>
<%= hidden_field_tag 'params[group_id]', "#{#group.id}" %>
<div class="field">
<%= collection_select('params', 'product_id', Product.where(:group_id => nil), :id, :name) %>
</div>
<div class="actions">
<%= submit_tag "assign", :name => nil %>
</div>
<% end %>
and in the groups_controller.rb I have
def add_reference
#group = Group.find(params[:group_id])
#product = Product.find(params["product_id"])
#group.products << #product
end
-I've read Detailed Association Reference chapter of association_basics in railsguides, and since it has collection<<(object) function added, I though I should be able to do something close to above. (which is not working because #product seems to be nil for some reason, and even if I set it to a valid ID and hardcode it, it fails on the line after that)
I think you may have answered this yourself :
If they can only be associate with one group at a time, do this :
def add_reference
#product = Product.find(params[:product_id])
#product.update_attribute("group_id", params[:group_id])
end
Moving a product from one group to another requires just one simple thing: Changing the product's group_id to "point" to the new group, that is, the group_id column should contain the new group's id after the change. Mr Trip's answer shows you the actual code.
I found out why it wasn't working.
in the above code that I wrote, the collection_select returns a hash itself.
therefore:
#product = Product.find(params[:product_id])
doesn't work (because all request parameters also come in a hash).
What I had to do was to access the recursive hash (not sure if the word is correct) like:
#product = Product.find(params[:params][:product_id])
Or, use where instead of find like:
#product = Product.where(params[:params]).limit(1)
and it works!!
Thank you for all of the comments. That kept me working on it.

Resources