Activerecord RubyonRails:棘手的多对多关系不起作用
我正在我的应用程序中设计一个“列表”映射功能,用于代码转换。 因此,我的mappins_list对象指向源_list对象和目标_list对象,这两个对象都是实例值_list类。 该设计看起来很经典,但它的表现与预期不符:Activerecord RubyonRails:棘手的多对多关系不起作用,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我正在我的应用程序中设计一个“列表”映射功能,用于代码转换。 因此,我的mappins_list对象指向源_list对象和目标_list对象,这两个对象都是实例值_list类。 该设计看起来很经典,但它的表现与预期不符: <td><%= link_to @mappings_list.source_list.name, @mappings_list.source_list%> 控制员在这里: class MappingsListsController < Appl
<td><%= link_to @mappings_list.source_list.name, @mappings_list.source_list%>
控制员在这里:
class MappingsListsController < ApplicationController
# Check for active session
before_action :signed_in_user
# Retrieve current current mapping
before_action :set_mappings_list, only: [:show, :edit, :update, :destroy]
# Retrieve all lists of values
before_action :set_values_lists
# GET /mappings_list
# GET /mappings_list.json
def index
@mappings_list = MappingsList.pgnd(current_playground).order("name")
end
# GET /mappings_list/1
# GET /mappings_list/1.json
def show
### Retrieved by Callback function
end
# GET /mappings_list/new
def new
@mappings_list = MappingsList.new
end
# GET /mappings_list/1/edit
def edit
### Retrieved by Callback function
end
# POST /mappings_list
# POST /mappings_list.json
def create
@mappings_list = MappingsList.new(mappings_list_params)
@mappings_list.updated_by = current_user.login
@mappings_list.created_by = current_user.login
@mappings_list.playground_id = current_user.current_playground_id
@mappings_list.owner_id = current_user.id
respond_to do |format|
if @mappings_list.save
format.html { redirect_to @mappings_list, notice: 'List of mappings was successfully created.' }
format.json { render action: 'show', status: :created, location: @mappings_list }
else
format.html { render action: 'new' }
format.json { render json: @mappings_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /mappings_list/1
# PATCH/PUT /mappings_list/1.json
def update
### Retrieved by Callback function
@mappings_list.updated_by = current_user.login
respond_to do |format|
if @mappings_list.update(mappings_list_params)
format.html { redirect_to @mappings_list, notice: 'List of mappings was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @mappings_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /mappings_list/1
# DELETE /mappings_list/1.json
def destroy
### Retrieved by Callback function
@mappings_list.destroy
respond_to do |format|
format.html { redirect_to mappings_lists_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
# Check for active session
def signed_in_user
redirect_to signin_url, notice: "You must log in to access this page." unless signed_in?
end
def set_mappings_list
@mappings_list = MappingsList.pgnd(current_playground).find(params[:id])
end
# retrieve the list of values lists
def set_values_lists
@lists_of_values = ValuesList.all
end
# Never trust mappings from the scary internet, only allow the white list through.
def mappings_list_params
params.require(:mappings_list).permit(:name, :description, :source_list_id, :target_list_id)
end
end
class-mappingslistscocontroller
双重引用值列表会使我的应用程序陷入麻烦吗
谢谢你的帮助
致以最良好的祝愿
Fred在类映射列表中
类顺序
@order=@customer.orders
好的,我明白了
在索引视图中,在处理实例列表时,我不能引用一个实例变量,如@mappings\u list
该代码适用于以下情况:
<td><%= link_to mappings_list.source_list.name, mappings_list.source_list%>
作为RoR的初学者,我希望我的解释是正确的
谢谢你的帮助
致以最良好的祝愿
Fred共享正在渲染视图的控制器(出现错误的地方)。我刚刚注意到,在创建第一个示例时,在模型中保存之前调用的set_code函数工作正常。因此外键是有效的,这是一个很好的观点。不,实际上,每个源列表可以通过映射列表链接到不同的目标列表。源列表有许多映射列表,我使用FK声明从父源列表和目标列表中检索字段,这在其他模块中同样适用。
# == Schema Information
#
# Table name: mappings_lists
#
# id :integer not null, primary key
# playground_id :integer
# code :string(255)
# name :string(255)
# description :text
# created_by :string(255)
# updated_by :string(255)
# owner_id :integer
# source_list_id :integer
# target_list_id :integer
# created_at :datetime
# updated_at :datetime
#
class MappingsList < ActiveRecord::Base
### scope
scope :pgnd, ->(my_pgnd) { where "playground_id=?", my_pgnd }
### before filter
before_create :set_code
### validation
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :description, length: { maximum: 1000 }
validates :created_by , presence: true
validates :updated_by, presence: true
validates :playground_id, presence: true
belongs_to :playground
validates :playground, presence: true # validates that the playground exists
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id" # helps retrieving the owner name
belongs_to :source_list, :class_name => "ValuesList", :foreign_key => "source_list_id" # helps retrieving the source list name
belongs_to :target_list, :class_name => "ValuesList", :foreign_key => "target_list_id" # helps retrieving the target list name
has_many :mappings
### private functions definitions
private
### before filters
def set_code
self.code = "#{source_list.code}_TO_#{target_list.code}"
end
end
resources :values_lists do
resources :values
resources :mappings_lists
end
class MappingsListsController < ApplicationController
# Check for active session
before_action :signed_in_user
# Retrieve current current mapping
before_action :set_mappings_list, only: [:show, :edit, :update, :destroy]
# Retrieve all lists of values
before_action :set_values_lists
# GET /mappings_list
# GET /mappings_list.json
def index
@mappings_list = MappingsList.pgnd(current_playground).order("name")
end
# GET /mappings_list/1
# GET /mappings_list/1.json
def show
### Retrieved by Callback function
end
# GET /mappings_list/new
def new
@mappings_list = MappingsList.new
end
# GET /mappings_list/1/edit
def edit
### Retrieved by Callback function
end
# POST /mappings_list
# POST /mappings_list.json
def create
@mappings_list = MappingsList.new(mappings_list_params)
@mappings_list.updated_by = current_user.login
@mappings_list.created_by = current_user.login
@mappings_list.playground_id = current_user.current_playground_id
@mappings_list.owner_id = current_user.id
respond_to do |format|
if @mappings_list.save
format.html { redirect_to @mappings_list, notice: 'List of mappings was successfully created.' }
format.json { render action: 'show', status: :created, location: @mappings_list }
else
format.html { render action: 'new' }
format.json { render json: @mappings_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /mappings_list/1
# PATCH/PUT /mappings_list/1.json
def update
### Retrieved by Callback function
@mappings_list.updated_by = current_user.login
respond_to do |format|
if @mappings_list.update(mappings_list_params)
format.html { redirect_to @mappings_list, notice: 'List of mappings was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @mappings_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /mappings_list/1
# DELETE /mappings_list/1.json
def destroy
### Retrieved by Callback function
@mappings_list.destroy
respond_to do |format|
format.html { redirect_to mappings_lists_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
# Check for active session
def signed_in_user
redirect_to signin_url, notice: "You must log in to access this page." unless signed_in?
end
def set_mappings_list
@mappings_list = MappingsList.pgnd(current_playground).find(params[:id])
end
# retrieve the list of values lists
def set_values_lists
@lists_of_values = ValuesList.all
end
# Never trust mappings from the scary internet, only allow the white list through.
def mappings_list_params
params.require(:mappings_list).permit(:name, :description, :source_list_id, :target_list_id)
end
end
<td><%= link_to mappings_list.source_list.name, mappings_list.source_list%>