Model 是这样的
class Contest < ActiveRecord::Base
has_many :contest_problems
has_many :problems, through: :contest_problems
end
class Problem < ActiveRecord::Base
has_many :contest_problems
has_many :contests, through: :contest_problems
end
class ContestProblem < ActiveRecord::Base
belongs_to :contest
belongs_to :problem
end
Migration 是这样的
class CreateContestProblems < ActiveRecord::Migration
def change
create_table :contest_problems do |t|
t.integer :contest_id
t.integer :problem_id
t.string :index, limit: 2
t.timestamps
end
end
end
现在想干这么一个事情:
c = Contest.find(1)
p = c.problems.find(1).index
Rails 在做这个查询的时候,INNER JOIN 了 contest_problems
,但是却不会把 contest_problems.*
给 SELECT 进来,所以没办法做这个查询。有什么好的解决方法吗?