更新
我的节目功能
def show
@contact = Contact.find(params[:id])
@data = Register.all :include => {:session =>[:term, :course]} , :conditions => ["contact_id = ?", params[:id]]
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @contact }
end
端
模型
class Register < ActiveRecord::Base
belongs_to :session
belongs_to :contact
end
class Session < ActiveRecord::Base
belongs_to :term
belongs_to :course
has_many :registers
has_many :contacts, :through => :registers
end
嗨,
我对铁轨上的红宝石有点新意。我想显示以下内容 -
- !ruby/object:Register
attributes:
created_at: 2010-06-20 11:39:06
updated_at: 2010-06-20 11:39:06
session_id: "32"
contact_id: "601"
id: "1"
attributes_cache: {}
session: !ruby/object:Session
attributes:
created_at: 2010-06-19 10:16:13
term_id: "26"
updated_at: 2010-06-19 10:16:13
id: "32"
course_id: "4"
attributes_cache: {}
course: !ruby/object:Course
attributes:
created_at: 2010-05-30 14:36:24
updated_at: 2010-05-30 14:36:28
course_name: Beginner
id: "4"
course_type: Running
attributes_cache: {}
term: &id001 !ruby/object:Term
attributes:
number: "1"
start_date: "2010-06-19"
created_at: 2010-06-19 10:16:13
updated_at: 2010-06-19 10:16:13
id: "26"
attributes_cache: {}
我认为我做错了
<% @data.Register.each do |c| %>
<tr>
<td><%=h c.Term.number %></td>
<td><%=h c.Course.course_name %></td>
</tr>
<% end %>
由于
答案 0 :(得分:1)
Ruby on Rails约定是类是大写的,类的实例不是。
如果模型注册包含声明
has_one term
然后您可以使用
访问该术语registration = Registration.find xxx # get an instance of the Registration model
registration.term # access the associated term model
# do not use registration.Term
因此...
1)您需要向我们展示您的控制器代码。你的ActiveRecord类文件 - 你是否使用belongs_to,has_many声明正确定义了关系?
2)你的sw应该更像:
# Controller
def show
# Shows all the registrations for person_id
# args: params['person_id']
@registrations = Register.find_all_by_person_id(params['person_id'].to_i_
end
# View
<% @registrations.each do |r| %>
<tr>
<td><%=h r.term.number %></td>
<td><%=h r.course.course_name %></td>
</tr>
<% end %>
ADDED
另外,要非常小心命名模型“会话” - 潜在的问题是,如果启用了db-backed会话,CGI会话可能会存储到模型会话中。