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”
cards
id => 1
name => “Captain Carl”
id => 2
name => “Whatever”
usercardsjoin
card_id => 1
user_id => 1
card_id => 2
user_id=> 1
card_id => 1
user_id => 2
Although i know how it should look like because of my experience with
PHP i have absolutely no clue how i can get this one to work.
Can any of you explain this or send me a link which handles this
particular issue?
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
end
user.rb
class User < ActiveRecord::Base
has_many :cardlevels
has_many :cards, :through => :cardlevels
end
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.
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.
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 %>
<% 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.
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!
[FORGET THIS PART, I FORGOT THE :TROUGH]
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?
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.
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 %>
<%= cl.other_data %>
<% end %>
You might need to check for cl.card nil though.
Colin
You’re my savior, thanks alot! It’s more the train of thought that needs
correcting than my code i guess.
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.
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 %>
<%= cl.other_data %>
<% end %>
You might need to check for cl.card nil though.
Colin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.