Reputation: 177
I have a problems that I'm not sure how best to approach. I have three tables in my database that I need to retrieve data from and display.
Tables: Variety
, Trial
, Result
I have a form:
<%= simple_form_for :search, url: vpc_search_path do |f| %>
<%= f.input :variety_one, collection: @variety, :include_blank => false %>
<%= f.input :variety_two, collection: @variety, :include_blank => false %>
<%= f.input :irrigations, collection: @irrigations, as: :check_boxes, :input_html => {:checked => true} %>
<%= f.input :years, collection: @years, as: :check_boxes, :input_html => {:checked => true} %>
<%= f.input :regions, collection: @regions, as: :check_boxes, :input_html => {:checked => true} %>
<%= f.button :submit %>
<% end %>
My Controller
class VpcController < ApplicationController
def index
all = Result.select(:variety_id)
@variety = Variety.where(:variety_id => all).order('variety_name DESC').pluck(:variety_id)
@years = Result.select('DISTINCT year').pluck(:year)
@regions = Trial.select('DISTINCT region_id').pluck(:region_id)
@irrigations = Trial.select('DISTINCT irrigated').pluck(:irrigated)
end
def search
@search = params[:search]
end
end
My Model
class Vpc < ActiveRecord::Base
has_many :varieties
has_many :results
has_many :trials
end
What I need is once the search form is complete it displays results in a table:
Variety One | Variety Two | Difference
Upvotes: 0
Views: 110
Reputation: 2369
If you need to create/update multiple objects in one form, then I suggest you to create pseudo-Model without connection to database, for example:
class Node
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :content, :variety, :region, :etc
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
def save
# here you do save of your data to distinct models
# for example
named_model = NamedModel.create! name: name, variety: variety
details = Details.create! region: region, etc: etc
content_holder = ContentHolder.create! content: content, named_id: named_model.id, details_id: details.id
end
end
And use it like normal active model:
<%= form_for @node do |f| %>
<% f.text_field :name %>
<% f.text_area :content %>
<% f.text_field :variety %>
<% f.text_field :region %>
<% f.text_area :etc %>
<% f.submit %>
<% end %>
Upvotes: 0
Reputation: 51
There is little more to be done to achieve what you want. In the search method instead of getting all the params, get the varieties variety_1 and variety_2 params, find then in the database and use the associations to find the results,then compute the difference. Create a view and display the information.
Upvotes: 1