2016-08-27 6 views
0

私は政治的なRailsアプリケーションを作成しています。これにより、候補者の問題の位置を比較することができます。その中心にはActive Recordテーブルを表すテーブルがあり、行は5つの大統領候補を表し、列は問題を表します。各<td>は、特定の候補者が問題の位置を記載しているかどうかを表します。もしそうでなければ、もちろん、それは無くなるでしょう。Rails - ActiveRecord Issue

問題:

私はcandidate.issues.eachとissue.positionのためのコードを持っているが、私は他のものを上書きせずに候補者のための新たなポジションを作るように見えることはできません。

目標は、候補者と両方のポジションを含めることで、ユーザーは問題のどこに立っているかを詳しく見ることができます。

ActiveRecord::Schema.define(version: 20160827005554) do 

create_table "candidates", force: :cascade do |t| 
    t.string "fname" 
    t.string "lname" 
    t.string "title" 
    t.string "minitial" 
    t.string "party" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

create_table "issues", force: :cascade do |t| 
    t.string "issname" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "candidate_id" 
    t.string "position" 
end 

end 

モデル:has_manyの問題(あるいは、それはそれぞれの問題のために位置すべきですか?)

例として、Railsのコンソールで:問題は、候補者に

候補に属し

i = Issue.find(1) *Agriculture 

c = Candidate.find(2) *Trump 

c.i.position = “(a couple paragraphs of text)” => error. 

例2、コンソールでも:

i = Issue.find(1) *Agriculture 

i.candidate_id = 1 *Clinton 

i.position = “(some paragraphs of text)" 

i.save 

i.candidate_id = 2 *Trump 

i.position = “(some other paragraphs of text)" 

i.save *overrides candidate_id(1) and, with it, Clinton’s position. 

ところで、私は問題の詳細な位置を与えることができるように、段落をコンソールに保存するつもりです。 <b>タグ、<i>タグなどを含めることは可能ですか?

Ex。農業上のクリントン:

エネルギーが

クリントンは、クリーンなエネルギー源の利点を売り込んだし、農業経済のための恩恵として、それらを仮定しています。最も重要なことは、彼女の計画では、2021年までに太陽電池パネルを5億倍に拡張することが求められている。彼女はバイオ燃料の研究と利用の拡大を支援している。

FOODポリシー

クリントンが資金を提供し、補足栄養支援プログラム(SNAP)のプログラムのように、連邦食品のプログラムを維持好みます。彼女はまた、農民に直接消費者をつなぐことを目的としたグラント、USDAマーケティングプログラムのための資金の増加、およびその他の特定されていない手段を通じ、地方の食糧オプションを増やしたいと考えています。

彼女は遺伝的に修飾された生物(GMOs)の使用を支持しています。

答えて

0

問題を一意に保ちたいと同時に、問題の候補者のそれぞれの位置を保存したいと思われるようです。

これは、テーブルの問題を複製する場合にのみ機能します。このように:

trump = Candidate.find 1 
clinton = Candidate.find 2 

Issue.create(issname: 'Agriculture', candidate: trump, popsition: 'some text') 
Issue.create(issname: 'Agriculture', candidate: clinton, position: 'text') 

私にとっては理想的ではありません。私は最終的に何

agriculture = Issue.find_by(issname: 'Agriculture') 
trump = Candidate.find_by(lname: 'Trump') 
trump.positions.where(issue: agriculture).create!(text: 'text') 
+0

:あなたはこのように書くことができるよりもPosition

class Position belongs_to :candidate belongs_to :issue end class Candidate has_many :positions end class Issue has_many :positions end create_table "positions", force: :cascade do |t| t.integer "issue_id" t.integer "candidate_id" t.string "text" t.datetime "created_at", null: false t.datetime "updated_at", null: false end 

- あなたはより良い別のモデルを追加すると思います。 –