![](/img/trans.png)
[英]Rails has_many :through association: save instance into join table
[英]Issue with join table and has_many through association in Rails 4
伙計們,這是我的數據庫結構。 AccountGrade.rb MODEL其帳戶和成績模型的聯接表
class AccountGrade < ActiveRecord::Base
belongs_to :account
belongs_to :grade
attr_accessor :grade
attr_accessor :section
end
我的account.rb模型
class Account < ActiveRecord::Base
has_many :grades, :through => :account_grades
has_many :account_grades
belongs_to :user
validates :school_name,:line_1,:city,:state,:country,:pincode,:phone_number, :presence => true
validates :pincode,:phone_number,numericality: { only_integer: true }
end
我的grade.rb模型
class Grade < ActiveRecord::Base
has_many :accounts, :through => :account_grades
has_many :account_grades
attr_accessor :account_grades
end
我的grades_controller.rb
class GradesController < ApplicationController
def index
@grades = Grade.all
render json: {
Grades:@grades
}.to_json
end
def add_class_sections
# unless params[:section]
@account_grades = AccountGrade.new class_sections_params
puts "Grades are #{@account_grades}"
@grades.each do |grade|
@account_grades = grade
puts grade
puts @account_grades
end
# end #unless ends here
end #function add_class_sections ends here
private
def class_sections_params
params.permit!
# params.require(:gardes).permit(:section)#, :email, :password, :salt, :encrypted_password)
end
end #class ends here
我在終端跟蹤中收到以下錯誤。
Started POST "/add_classes?grade_id[0]=1§ion[0]=2&grade_id[1]=2§ion[1]=1&grade_id[2]=3§ion[2]=1" for 127.0.0.1 at 2015-11-17 12:43:47 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by GradesController#add_class_sections as */*
Parameters: {"grade_id"=>{"0"=>"1", "1"=>"2", "2"=>"3"}, "section"=>{"0"=>"2", "1"=>"1", "2"=>"1"}}
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.8ms)
ActiveRecord::UnknownAttributeError (unknown attribute 'controller' for AccountGrade.):
app/controllers/grades_controller.rb:11:in `add_class_sections'
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (11.0ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (24.8ms)
哇,您的代碼有很多問題。
不管您的推理如何,這都是您應該如何使它工作的...
#app/controllers/grades_controller.rb
class GradesController < ApplicationController
def index
@grades = Grade.all
respond_to do |format|
format.json { render json: @grades.to_json }
format.html
end
end
def create
@account_grades = AccountGrade.new class_sections_params
redirect_to @account.grades if @account_grades.save
end
private
def sections_params
params.require(:grades).permit(:section)
end
end
如果您可以使用此代碼,那么您將可以更輕松地了解為什么它要尋找controller
屬性(這是一個奇怪的錯誤)。
您還應該了解Rails中的一些約定:
puts
在控制器 -你有Rails.logger.info()
輸出到控制台。 add_class_section
,而是應根據需要使用new
和create
方法。 -
另外, 刪除所有attr_accessor
引用 ,尤其是與任何關聯名稱沖突的引用 。
attr_accessor
基本上為模型聲明了新的getter
/ setter
方法。 如果這些方法重寫了任何關系,則實際上將阻止它們起作用。
除非要創建一個半永久屬性(即不保存到數據庫的IE),否則不需要attr_accessor
。 Rails中attr_accessor
的最常見用法是創建虛擬屬性 :
這是我想要實現的。 歡迎提供有關重構或改善以下代碼的任何建議。
class GradesController < ApplicationController
before_filter :authenticate, only: [:create]
def index
@grades = Grade.all
render json: @grades.to_json
end
def create
@section_name_counter = 0
@section_names=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",
"T","U","V","W","X","Y","Z"]
@account_grades = AccountGrade.new #sections_params
@account_grades.grade_id = params[:grade_id]
@account_grades.account_id = params[:account_id]
@account_grades.save!
while @section_name_counter < params[:section].to_i do
@section = Section.new
@section.account_grade_id = @account_grades.id
@section.name = @section_names[@section_name_counter]
@section.save!
@section_name_counter += 1
end
render json: @account_grades if @section.save
end
private
def sections_params
params.require(:grade_id).permit(:section,:account_id)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.