开发者

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. Scenarios represent a point in time and contain various other informatio开发者_开发技巧n. 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? Originally I set up scaffolding for the Notes like so:

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

class Note < ActiveRecord::Base
    has_many :scenarios
end

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜