自定义左连接与魔术轨道getter

时间:2015-05-05 16:56:12

标签: ruby-on-rails activerecord left-join rails-activerecord

如何在rails中使用这种查询?

Question.joins("left join answers a on a.question_id = questions.id and a.user_id = #{current_user.id}").select('questions.*, a.*')

我想使用question.answer或一些魔法吸气剂。

1 个答案:

答案 0 :(得分:0)

您可以使用JOIN将查询转换为使用子查询进行查询:

Question

在这种情况下,您可以使用questions.each do |question| p question.answer ... 对象及其所有关联:

 [System.Web.Http.HttpPost]
        public HttpResponseMessage InsertSchoolBranch(JObject jsonData)
        {

            try
            {
                dynamic json = jsonData;    
                JObject jbranchInfo = json.branchInfo;
                JObject jbranchPolicy = json.branchPolicy;


                var branchInfo = jbranchInfo.ToObject<SchoolBranch>();
                var branchPolicy = jbranchPolicy.ToObject<SchoolPolicy>();

                int schoolId = Convert.ToInt32(UserDataPieces(2));
                int userId = Convert.ToInt32(UserDataPieces(0));

                unitOfWork.SchoolManagerRepository.InsertSchoolBranch(branchInfo, branchPolicy, userId, schoolId, ref message);

                return new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch (UnauthorizedAccessException)
            {
                return Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);


            }


        }