我有3个表:成员,会议,meeting_has_members。 模型是:
member.rb
import pandas as pd
import numpy as np
import scipy.fftpack
# sample points
N = 1024
# sample spacing
T = 1.0/512.0
# example function
x = np.linspace(0.0, N*T, N)
y = 1.5 * np.sin(5*2*np.pi*x) + 3 * np.sin(6*2*np.pi*x)
df = pd.DataFrame({'X': x, 'Y': y})
sumy = sum(abs(df['Y'])**2)
df['FFT (Y)'] = scipy.fftpack.fft(df['Y'])/(N/2)
df['FFT (freq)'] = scipy.fftpack.fftfreq(N, T)
sumffty = sum(abs(df['FFT (Y)'])**2)
print sumy, sumffty
meeting.rb
class Member < ActiveRecord::Base
has_many :meeting_has_members
has_many :meetings, through: :meeting_has_members
accepts_nested_attributes_for :meeting_has_members
accepts_nested_attributes_for :meetings
meeting_has_member.rb
class Meeting < ActiveRecord::Base
has_many :meeting_has_members
has_many :members, through: :meeting_has_members
accepts_nested_attributes_for :meeting_has_members
accepts_nested_attributes_for :members
我尝试做的是,在创建会议时,能够向其添加成员,并在提交时创建关系meeting_has_member。 表meeting_has_members有2列:member_id,meeting_id。
这就是meeting_controller:
meeting_controller.rb
class MeetingHasMember < ActiveRecord::Base
belongs_to :member
belongs_to :meeting
创建会议的表格是:
会议/ _form.html.erb
class MeetingsController < ApplicationController
before_action :set_meeting, only: [:show, :edit, :update, :destroy]
def index
@meetings = Meeting.all
end
def show
end
def new
@meeting = Meeting.new
@meeting_has_members = @meeting.meeting_has_members.build
end
def edit
end
def create
@meeting = Meeting.new(meeting_params)
respond_to do |format|
if @meeting.save
format.html { redirect_to @meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: @meeting }
else
format.html { render :new }
format.json { render json: @meeting.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @meeting.update(meeting_params)
format.html { redirect_to @meeting, notice: 'Meeting was successfully updated.' }
format.json { render :show, status: :ok, location: @meeting }
else
format.html { render :edit }
format.json { render json: @meeting.errors, status: :unprocessable_entity }
end
end
end
def destroy
@meeting.destroy
respond_to do |format|
format.html { redirect_to meetings_url, notice: 'Meeting was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_meeting
@meeting = Meeting.find(params[:id])
end
def meeting_params
params.require(:meeting).permit(:meeting_name, :meeting_description, :meeting_date, :agenda_id,
meeting_has_members_attributes: [:id],
members_attributes: [:id])
end
end
当我提交时,它会在meeting_has_members表meeting_id上创建,但它没有设置member_id的值,应该以嵌套的形式选择。
任何人都可以帮我吗?
答案 0 :(得分:1)
您的meeting_params
应该是这样的
def meeting_params
params.require(:meeting).permit(:meeting_name, :meeting_description, :meeting_date, :agenda_id, meeting_has_members_attributes: [:id, :member_id])
end
请注意,我在:member_id
中添加了meeting_has_members_attributes
并删除了members_attributes
,因为您没有保存它们。