How to access Active Record Object Values ? Ruby on Rails -
i relatively new ruby on rails. made relationship called friendships, has following attributes:
user_id, friend_id, state
inside friendship.rb model file, have created opposite method me inverse friendships:
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
inside view file have:
<h1> requests ive sent: </h1> <%@sent_friendships.each |x|%> <%=x.opposite%><br> <%=x.opposite.inspect%> <%end%>
note: @sent_friendships defined in controller as: @sent_friendships=current_user.friendships.where(state:'pending');
view output:
x.opposite:
#<friendship::activerecord_relation:0x007fd8abcf3498>
x.opposite.inspect:
#<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">]>
but after calling x.opposite.inspect, how can access specific attributes, example state? if try x.opposite.state error:
undefined method `state' #<friendship::activerecord_relation:0x007fd8a3f612f0>
i confused? clear .state attribute after calling inspect method? please?!
what returning opposite using active record 'where' activerecord::relation array structure. x.opposite.first.state
should want.
Comments
Post a Comment