Re: [Rails] Re: Associations join
On 20 June 2012 08:51, Timen E. <lists@ruby-forum.com> wrote:
> I've just got a really basic setup to see if i got it working:
>
>
> card.rb
> class Card < ActiveRecord::Base
> attr_accessible :attack, :defense, :name
> has_many :cardlevels
> has_many :users, :through => :cardlevels
> end
>
> cardlevel.rb
> class Cardlevel < ActiveRecord::Base
> attr_accessible :card_id, :level, :user_id
> belongs_to :user
> belongs_to :card
> end
>
> user.rb
> class User < ActiveRecord::Base
> attr_accessible :name
> has_many :cardlevels
> has_many :cards
> end
> -----------------------------------------------------
>
> card_controller.rb
> class CardController < ApplicationController
> def show
> @card = Card.all
You would have been better to put @cards = Card.all as the result will
be be an (effectively) an array of cards not a single one.
> end
> end
>
> User & cardlevel are empty for now
> -----------------------------------------------------
>
> <h1>Card#show</h1>
> <p>Find me in app/views/card/show.html.erb</p>
> <%= @card.user %>
> Where i tried eaching the @card, tried @card.users, @card.user.name etc.
> etc.
> -----------------------------------------------------
>
> The error:
> Showing c:/tuts/kaartje/app/views/card/show.html.erb where line #3
> raised:
>
> undefined method `user' for #<Array:0x3c81ac8>
The clue is in the error message, as is often the case, it says that
@card is an Array and the Arrray class does not have a method user.
You can only call user on an individual card.
Colin
> Extracted source (around line #3):
>
> 1: <h1>Card#show</h1>
> 2: <p>Find me in app/views/card/show.html.erb</p>
> 3: <%= @card.user %>
> Rails.root: c:/tuts/kaartje
>
> Application Trace | Framework Trace | Full Trace
> app/views/card/show.html.erb:3:in
> `_app_views_card_show_html_erb___1042037435_30519240'
> Request
>
> Parameters:
>
> {"id"=>"show"}
> -----------------------------------------------------
>
> --
> 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 rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home