find(:all) 然后将另一个表中的数据添加到对象中

发布于 2024-08-22 04:45:44 字数 2510 浏览 5 评论 0原文

我有两个表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-08-29 04:45:44

当您调用

render :xml  => @friendrequests

Rails 时,Rails 会迭代数组中的每个元素并调用 ActiveRecord::Base#to_xml 方法。
同样,:json 选项表示对 ActiveRecord::Base#to_json 的调用。

to_xmlto_json 有一个 :methods 选项,其中包含要包含在序列化中的方法数组。

解决方案非常简单。重写这些方法。

class Friendship

  # ...

  def firstname
    # here get the first name
  end

  def lastname
    # here get the last name
  end

  def to_xml(options = {})
    options[:methods] ||= [:firstname, :lastname]
    super(options)
  end

  def to_json(options = {})
    options[:methods] ||= [:firstname, :lastname]
    super(options)
  end

end

When you call

render :xml  => @friendrequests

Rails iterates each element in the array and calls the ActiveRecord::Base#to_xml method.
Likewise, the :json option means a call to ActiveRecord::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.

class Friendship

  # ...

  def firstname
    # here get the first name
  end

  def lastname
    # here get the last name
  end

  def to_xml(options = {})
    options[:methods] ||= [:firstname, :lastname]
    super(options)
  end

  def to_json(options = {})
    options[:methods] ||= [:firstname, :lastname]
    super(options)
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文