我有两个模型:
class Sentence < ActiveRecord::Base
attr_accessible :sentence_id, :authority_name #...
end
class Rule < ActiveRecord::Base
attr_accessible :description, :headline, :note, :sentence_id
end
我想知道如何创建belongs_to :sentence
关联,Rule
其行为类似于此伪 SQL 代码:
SELECT * FROM rules
INNER JOIN sentences ON rules.sentence_id = sentences.sentence_id;
编辑:
我想得到类似的东西
rule = Rule.find 797
# we all know how SQL query will look like...
rule.sentence
# => SELECT * FROM sentences
INNER JOIN rules ON rules.sentence_id = sentences.sentence_id
WHERE rules.id = 797