0

どのようにアプローチするのが最善なのかわからないという問題があります。データベースには、データを取得して表示する必要がある 3 つのテーブルがあります。

テーブル: VarietyTrialResult

私はフォームを持っています:

<%= 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 %>

私のコントローラー

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

私のモデル

class Vpc < ActiveRecord::Base
  has_many :varieties
  has_many :results
  has_many :trials
end

必要なのは、検索フォームが完了すると、結果が表に表示されることです。

Variety One |  Variety Two  | Difference
4

2 に答える 2

1

あなたが望むものを達成するためにやるべきことはあと少しです。検索メソッドでは、すべてのパラメータを取得する代わりに、品種variable_1およびvariable_2パラメータを取得し、データベースで見つけて、関連付けを使用して結果を見つけ、差を計算します。ビューを作成して情報を表示します。

于 2013-10-30T08:31:36.867 に答える
0

1 つのフォームで複数のオブジェクトを作成/更新する必要がある場合は、データベースに接続せずに疑似モデルを作成することをお勧めします。次に例を示します。

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

通常のアクティブ モデルのように使用します。

<%= 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 %>
于 2013-10-30T19:57:52.183 に答える