好的,这是一个三重表查询,我对如何解决这个问题感到非常困惑,请继续构建它。
我正在尝试找到与特定电子邮件地址相关的所有辅导课程。
表格信息列在下方。
User,TutorSession,& SessionUser。
TutorSession属于User
用户有很多SessionUser
以下是每个表的属性:
用户
:id => :integer,
:email => :string,
:encrypted_password => :string,
:reset_password_token => :string,
:reset_password_sent_at => :datetime,
:remember_created_at => :datetime,
:sign_in_count => :integer,
:current_sign_in_at => :datetime,
:last_sign_in_at => :datetime,
:current_sign_in_ip => :string,
:last_sign_in_ip => :string,
:created_at => :datetime,
:updated_at => :datetime,
:first_name => :string,
:last_name => :string,
:provider => :string,
:uid => :string,
:roles_mask => :integer,
:last_activity_at => :datetime,
:time_zone => :string,
:imported => :boolean,
:authentication_token => :string,
:getting_started_dismissed => :boolean,
:schedule_valid => :datetime
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["first_name"], :name => "index_users_on_first_name"
add_index "users", ["last_name"], :name => "index_users_on_last_name"
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
TutorSession
:id => :integer,
:session_code => :string,
:name => :string,
:moderator_pw => :string,
:attendee_pw => :string,
:session_type => :string,
:status => :string,
:started_at => :datetime,
:ended_at => :datetime,
:tutor_pickup_type => :string,
:recording_url => :string,
:created_at => :datetime,
:updated_at => :datetime,
:requested_by => :integer,
:subject_full_name => :string,
:student_dismissed => :boolean,
:school_id => :integer,
:tutoring_subject_id => :integer,
:tutor_appointment_id => :integer,
:white_board_status => :string,
:session_length => :integer
SessionUser
t.integer "tutor_session_id"
t.integer "user_id"
t.string "usertype"
t.string "url"
t.datetime "login_at"
t.string "logout_at"
t.string "flag"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "user_agent_string"
t.string "browser"
t.string "os"
end
add_index "session_users", ["tutor_session_id"], :name => "index_session_users_on_session_id"
add_index "session_users", ["user_id"], :name => "index_session_users_on_user_id"
修改
我在说明表格如何相互关联时犯了一个错误。对不起,我的脑袋在别的地方。
SessionUser 属于用户和 tutor_session
TutorSession 有很多 session_users
用户有多个 session_users
答案 0 :(得分:1)
不确定您的模型是什么样的,但应该像以下一样简单:
User.find_by_email('someone@example.com').tutor_sessions
为此,您的模型应该如下所示:
class User < ActiveRecord::Base
has_many :sessions, :class_name => SessionUser
has_many :tutor_sessions, :through => :sessions
end
答案 1 :(得分:0)
基本上,这个查询:
SELECT s.*
FROM users u
JOIN session_user su ON u.id = su.user_id
JOIN tutor_session s ON s.id = su.tutor_session_id
WHERE u.email = 'foo@bar'