English 中文(简体)
铁路3.2 活性记录 记录不显示
原标题:Rails 3.2 Active Record Join: Record not showing up

我的模型有着许多与人的关系,使教师们能够把许多团队连在一起,一个小组可以有多个教练。 (助理、主管等)

铁路运输公司,我运营:

 @coach = Coach.joins(:teams).select("coaches.first_name, coaches.last_name, teams.team_level")

返回:

 => [#<Coach first_name: "john", last_name: "doe">] 

请注意,它打算退回<代码>teams.team_level,因此,我认为,我必须使用@coach.team_level

When I do .to_sql it 返回:

=> "SELECT coaches.first_name, coaches.last_name, teams.team_level 
FROM `coaches` 
INNER JOIN `coach_teams` ON `coach_teams`.`coach_id` = `coaches`.`id` 
INNER JOIN `teams` ON `teams`.`id` = `coach_teams`.`team_id`

这是我所期望的。 因此,当我对我的行文进行 run时,我就有了预期的领域。

我在这里做了什么错误,我看不到什么? 感谢对这一点的思考!

最佳回答

你没有做任何错误,你使用<代码>的方法。 Coach 模型,请见Coach

由于你重新使用团队,你可以查阅<代码>team_level。 无附加疑问的价值。

问题回答

这实际上是好的。 你们要求采用一种辅导模式,你们加入小组模式。

因此,你实际上需要<代码>@coach.teams.team_level才能进入团队一级。

选择可以改变你的模型图象,它只能过滤回归的特性表,这样,你需要较少的圆形和较少的数据传输,但结构没有变化。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签