简体   繁体   English

RoR 3 has_many / belongs_to或has_and_belongs_to_many?

[英]RoR 3 has_many/belongs_to or has_and_belongs_to_many?

I have two tables in a Rails app that I'm using to track performance - Scenarios and Notes. 我在Rails应用程序中有两个用于跟踪性能的表-场景和便笺。 Scenarios represent a point in time and contain various other information. 场景代表一个时间点,并包含各种其他信息。 Notes contain some information about the differences between two Scenarios. 注释包含有关两种方案之间差异的一些信息。 What's a good way to set up my table schema so that it's easy to access a Note given two Scenarios? 设置我的表架构的一种好方法是什么,以便在两种情况下都可以轻松访问Note? Originally I set up scaffolding for the Notes like so: 最初,我为Notes设置了脚手架,如下所示:

rails generate scaffold Note scenarios_a:references scenarios_b:references data:text

This presents a couple problems. 这带来了几个问题。 The first problem is that it's a little unwieldy to search for notes given two scenarios. 第一个问题是在两种情况下搜索笔记有点麻烦。 The other problem I'm seeing now is when trying to display notes, I get this error: 我现在看到的另一个问题是尝试显示笔记时,出现此错误:

NameError (uninitialized constant Note::ScenarioA):

What's the correct way to do this? 正确的方法是什么?

app/models/note.rb app / models / note.rb

class Note < ActiveRecord::Base
    has_many :scenarios
end

app/models/scenario.rb app / models / scenario.rb

class Scenario < ActiveRecord::Base
    belongs_to :notes
end  

Accessing a note's scenarios: 访问便笺的场景:

  @note.scenarios

Accessing a scenario's note: 访问方案的注释:

 @scenario.note

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM