> undefined method `friends' for #<Array:0xb6144040>

You didn't show us the controller code that defines @customer but I'll bet you 
used something returned an array when you wanted a single object.

Also, be sure to note that Rails 3.1 finders return ARels (active record 
relation objects) which are array-like, pre-Rails 3.1 you get an actual array. 
So you get some funny business when upgrading from pre-Rails 3.1 to Rails 3.1 
or higher when doing stuff like this, because ARels can behave like Arrays but 
aren't actually arrays. 




On Aug 16, 2012, at 11:18 AM, Jean-Sébastien D. wrote:

> I am trying to develop a friendship system like facebook. I have follow
> for guidance the two following sources
> 
> How to Implement a Friendship Model in Rails 3 for a Social Networking
> Application?
> Railsspace book chapter 14, which can only be downloaded i believed
> I am a bit confused on the way railsspace has been building there
> relationship. What i am trying to do his to show in a views my
> pending_request, accepted_request and declined_request. The books seem
> to suggest the following relationship
> 
> FriendshipModel
> 
> class Friendship < ActiveRecord::Base
>  belongs_to :customer
>  belongs_to :friend, :class_name => 'Customer', :foreign_key =>
> 'friend_id'
> CustomerModel
> 
> class Customer < ActiveRecord::Base
> #RELATIONSHIP
>    has_many :friendships
>    has_many :friends, :through => :friendships,
>         :conditions => "status = 'accepted'"
>    has_many :requested_friends,
>         :through => :friendships,
>         :source => :friend,
>         :conditions => "status = 'requested'"
>    has_many :pending_friends,
>         :through => :friendships,
>         :source => :friend,
>         :conditions => "status = 'pending'"
> The issues i am having his how can i access friends who are pendings,
> friends who are requested. I have the following view in my index
> (friendship/index)
> 
> <div><h1>Request</h1></div>
> <table>
>  <% @customer.friends.each do |friend| %>
>  <tr>
>    <td><%= link_to friend.first_name, '#' %></td>
>  </tr>
>  <% end %>
> </table>
> But i get the following errors
> 
> NoMethodError in Friends#index
> 
> Showing /app/views/friends/index.html.erb where line #15 raised:
> 
> undefined method `friends' for #<Array:0xb6144040>
> Extracted source (around line #15):
> 
> 12:
> 13: <div><h1>Request</h1></div>
> 14: <table>
> 15: <% @customer.friends.each do |friend| %>
> 16: <tr>
> 17: <td><%= link_to friend.first_name, '#' %></td>
> 18: </tr>
> Mind you, my model is called friendship, my controller his called
> friends ( not the best name but its more for practise learning I did)
> 
> Here the model
> 
> # Table name: friendships
> #
> #  id          :integer         not null, primary key
> #  customer_id :integer
> #  friend_id   :integer
> #  created_at  :datetime        not null
> #  updated_at  :datetime        not null
> #  approved    :string(255)
> #
> Customer Schema
> 
> # Table name: customers
> #
> #  id                     :integer         not null, primary key
> #  email                  :string(255)     default(""), not null
> #  created_at             :datetime        not null
> #  updated_at             :datetime        not null
> #  first_name             :string(255)
> #  middle_name            :string(255)
> #  last_name              :string(255)
> #  isadmin                :boolean         default(FALSE)
> #  isMod                  :boolean         default(FALSE)
> #
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to