find(:all) 然后将另一个表中的数据添加到对象中
我有两个表:
create_table "friendships", :force => true do |t|
t.integer "user1_id"
t.integer "user2_id"
t.boolean "hasaccepted"
t.datetime "created_at"
t.datetime "updated_at"
end
我
create_table "users", :force => true do |t|
t.string "email"
t.string "password"
t.string "phone"
t.boolean "gender"
t.datetime "created_at"
t.datetime "updated_at"
t.string "firstname"
t.string "lastname"
t.date "birthday"
end
需要向用户显示 Friendrequests 列表,因此我在控制器中使用此方法:
def getfriendrequests
respond_to do |format|
case params[:id]
when "to_me"
@friendrequests = Friendship.find(:all, :conditions => { :user2_id => session[:user], :hasaccepted => false })
when "from_me"
@friendrequests = Friendship.find(:all, :conditions => { :user1_id => session[:user], :hasaccepted => false })
end
format.xml { render :xml => @friendrequests }
format.json { render :json => @friendrequests }
end
end
我几乎使用 AJAX 完成所有操作,因此要获取好友请求的名字和姓氏对于具有 UID user2_id
的用户(to_me
参数稍后出现,现在不用担心),我需要一个 for 循环来进行多个 AJAX 调用。这很糟糕并且花费大量带宽。所以我宁愿让 getfriendrequests
也返回相应用户的名字和姓氏,因此,例如 JSON 响应不会是:
[
{
"friendship": {
"created_at": "2010-02-19T13:51:31Z",
"user1_id": 2,
"updated_at": "2010-02-19T13:51:31Z",
"hasaccepted": false,
"id": 11,
"user2_id": 3
}
},
{
"friendship": {
"created_at": "2010-02-19T16:31:23Z",
"user1_id": 2,
"updated_at": "2010-02-19T16:31:23Z",
"hasaccepted": false,
"id": 12,
"user2_id": 4
}
}
]
而是:
[
{
"friendship": {
"created_at": "2010-02-19T13:51:31Z",
"user1_id": 2,
"updated_at": "2010-02-19T13:51:31Z",
"hasaccepted": false,
"id": 11,
"user2_id": 3,
"firstname": "Jon",
"lastname": "Skeet"
}
},
{
"friendship": {
"created_at": "2010-02-19T16:31:23Z",
"user1_id": 2,
"updated_at": "2010-02-19T16:31:23Z",
"hasaccepted": false,
"id": 12,
"user2_id": 4,
"firstname": "Mark",
"lastname": "Gravell"
}
}
]
我想到了 中的 for 循环>getfriendrequests
方法,但我不知道如何实现这个,也许有一个更简单的方法。它也必须适用于 XML。有人可以帮助我吗?谢谢
I have two tables:
create_table "friendships", :force => true do |t|
t.integer "user1_id"
t.integer "user2_id"
t.boolean "hasaccepted"
t.datetime "created_at"
t.datetime "updated_at"
end
and
create_table "users", :force => true do |t|
t.string "email"
t.string "password"
t.string "phone"
t.boolean "gender"
t.datetime "created_at"
t.datetime "updated_at"
t.string "firstname"
t.string "lastname"
t.date "birthday"
end
I need to show the user a list of Friendrequests
, so I use this method in my controller:
def getfriendrequests
respond_to do |format|
case params[:id]
when "to_me"
@friendrequests = Friendship.find(:all, :conditions => { :user2_id => session[:user], :hasaccepted => false })
when "from_me"
@friendrequests = Friendship.find(:all, :conditions => { :user1_id => session[:user], :hasaccepted => false })
end
format.xml { render :xml => @friendrequests }
format.json { render :json => @friendrequests }
end
end
I do nearly everything using AJAX, so to fetch the First and Last name of the user with UID user2_id
(the to_me
param comes later, don't worry right now), I need a for loop which make multiple AJAX calls. This sucks and costs much bandwidth. So I'd rather like that getfriendrequests
also returns the First and Last name of the corresponding users, so, e.g. the JSON response would not be:
[
{
"friendship": {
"created_at": "2010-02-19T13:51:31Z",
"user1_id": 2,
"updated_at": "2010-02-19T13:51:31Z",
"hasaccepted": false,
"id": 11,
"user2_id": 3
}
},
{
"friendship": {
"created_at": "2010-02-19T16:31:23Z",
"user1_id": 2,
"updated_at": "2010-02-19T16:31:23Z",
"hasaccepted": false,
"id": 12,
"user2_id": 4
}
}
]
but rather:
[
{
"friendship": {
"created_at": "2010-02-19T13:51:31Z",
"user1_id": 2,
"updated_at": "2010-02-19T13:51:31Z",
"hasaccepted": false,
"id": 11,
"user2_id": 3,
"firstname": "Jon",
"lastname": "Skeet"
}
},
{
"friendship": {
"created_at": "2010-02-19T16:31:23Z",
"user1_id": 2,
"updated_at": "2010-02-19T16:31:23Z",
"hasaccepted": false,
"id": 12,
"user2_id": 4,
"firstname": "Mark",
"lastname": "Gravell"
}
}
]
I thought of a for loop in the getfriendrequests
method, but I don't know how to implement this, and maybe there is an easier way. It must also work for XML. Can anyone help me? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
Rails 时,Rails 会迭代数组中的每个元素并调用
ActiveRecord::Base#to_xml
方法。同样,
:json
选项表示对ActiveRecord::Base#to_json
的调用。to_xml 和 to_json 有一个
:methods
选项,其中包含要包含在序列化中的方法数组。解决方案非常简单。重写这些方法。
When you call
Rails iterates each element in the array and calls the
ActiveRecord::Base#to_xml
method.Likewise, the
:json
option means a call toActiveRecord::Base#to_json
.Both to_xml and to_json have a
:methods
option which contains an array of methods to include in the serialization.The solution is really straightforward. Override these methods.