Ruby on rails 如何访问活动记录对象值?RubyonRails

Ruby on rails 如何访问活动记录对象值?RubyonRails,ruby-on-rails,object,ruby-on-rails-4,activerecord,model,Ruby On Rails,Object,Ruby On Rails 4,Activerecord,Model,我是RubyonRails的新手。我建立了一种称为友谊的关系,它具有以下属性: user_id, friend_id, state 在my Friendly.rb模型文件中,我创建了一个相反的方法来帮助我获得反向友谊: class Friendship < ActiveRecord::Base def opposite user = self.user_id; friend=self.friend_id; return Friendship.where(user

我是RubyonRails的新手。我建立了一种称为友谊的关系,它具有以下属性:

user_id, friend_id, state
在my Friendly.rb模型文件中,我创建了一个相反的方法来帮助我获得反向友谊:

class Friendship < ActiveRecord::Base

def opposite
    user = self.user_id;
    friend=self.friend_id;
    return Friendship.where(user_id:friend,friend_id:user);
  end

end
x、 相反。检查:

#<ActiveRecord::Relation [#<Friendship id: 4, user_id: 3, friend_id: 1, created_at: "2015-07-01 21:42:21", updated_at: "2015-07-01 21:42:21", state: "requested">]>
#
但是在调用x.contract.inspect之后,如何访问特定属性,例如仅访问状态?如果我尝试x.contract.state,则会出现以下错误:

undefined method `state' for #<Friendship::ActiveRecord_Relation:0x007fd8a3f612f0>
的未定义方法“state”#

我很困惑?调用inspect方法后.state是否是一个属性?救命啊

使用活动记录“where”从对方返回的是一个类似数组的ActiveRecord::Relation。所以
x.reversit.first.state
应该得到你想要的。

你能写下这个查询的结果吗->@sent\u friendships=current\u user.friendships
#<ActiveRecord::Relation [#<Friendship id: 4, user_id: 3, friend_id: 1, created_at: "2015-07-01 21:42:21", updated_at: "2015-07-01 21:42:21", state: "requested">]>
undefined method `state' for #<Friendship::ActiveRecord_Relation:0x007fd8a3f612f0>