Associations join

Hey there,

Im fairly new to rails and i've tried several tutorials which taught me
how to use the associatives on a basic level(has_many => belongs_to).

However i'm trying to figure out how i can create and execute some kind
of a join. My case is:
I've got a list of playing cards in my DB that are static.
Every user can have any of those cards, even multiple times. So my
tables would look something like this:

users
id = 1
name => "Bear Grylls"

id => 2
name => "Poopypants"

Check out the guide to associations. It should have more information than you need…

http://guides.rubyonrails.org/association_basics.html

Hey!

Associats are covered in great detail here:
http://guides.rubyonrails.org/association_basics.html

I think what you need is a has_many :through association

-- Manuel

Thanks you guys, i've actually tried that(And failed) Perhaps you guys
can help me out.
I have to following files:

card_controller.rb
class CardController < ApplicationController
  has_many :cardlevels
  has_many :users, :through => :cardlevels
  def show
    @cards = Card.find(1)
  end
end

I seem to have it up and running at this point. I made all the
has_many's etc. in my models. But when i try @cards.user (Or each it
first and use c.user) it says 'undefined method 'user''. So i'm assuming
i'm doing something awfully wrong.

My card_controller looks like this
def show
    @cards = Card.all
  end

Here are my models:
card.rb
class Card < ActiveRecord::Base
    has_many :cardlevels
  has_many :users, :through => :cardlevels
end

I seem to have it up and running at this point. I made all the
has_many's etc. in my models. But when i try @cards.user (Or each it
first and use c.user) it says 'undefined method 'user''. So i'm assuming
i'm doing something awfully wrong.

My card_controller looks like this
def show
@cards = Card.all
end

Here are my models:
card.rb
class Card < ActiveRecord::Base
has_many :cardlevels
has_many :users, :through => :cardlevels
end
-------------------------------------------------

cardlevel.rb
class Cardlevel < ActiveRecord::Base
belongs_to :users
belongs_to :cards

Those should both be singular (:user and :card). Each cardlevel
belongs to one user and card so they should be singular.

Colin

Colin Law wrote in post #1065209:

Post the code for the models, the code that generates the error (with
a few lines each side) and the full error message.

Colin

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 is a collection of cards (that's how you populate it in the
"show" action. If it needs to be a single card, you need to find it by
ID (or some other attribute), if it's supposed to be a collection of
every card, you need to iterate it in the view and print the user for
each card.

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

Michael Pavling wrote in post #1065301:

undefined method `user' for #<Array:0x3c81ac8>
Extracted source (around line #3):

@card is a collection of cards (that's how you populate it in the
"show" action. If it needs to be a single card, you need to find it by
ID (or some other attribute), if it's supposed to be a collection of
every card, you need to iterate it in the view and print the user for
each card.

aaah i see, nonetheless i noticed that i turned it around. I need to get
the user with it's cards, haha!

What you mentioned did work but it doesn't seem to 'associate' the right
way around because i'm getting the error SQLite3::SQLException: no such
column: cards.user_id: SELECT "cards".* FROM "cards" WHERE
"cards"."user_id" = 1"".
Shouldn't he query through cardlevels instead of cards?

Still got one(Hopefuly last for now) question, guys.

I've got the above data now and i can access my cards through users that
are joined by the cardlevel. I'm trying to loop through all the cards a
user has as such:
<% @user.cards.each do |c| %>
  <%= c.name %><br>
<% end %>

And this works great. However i'd like to get some extra info about this
card from my cardlevel array, but i can't seem to access it like this. I
can however loop it in a seperate each, but i don't want that obviously.

Any ideas?

Thanks

You have not explained well what you are trying to do, but possibly
you want to loop through the cardlevels instead of the cards, then for
each cardlevel you can obtain the card aswell as the other data, so
something like

<% @user.cardlevels.each do |cl| %>
<%= cl.card.name %><br>
<%= cl.other_data %>
<% end %>

You might need to check for cl.card nil though.

Colin

Colin Law wrote in post #1065311: