Array in database?

Hello, I wanted to know how to make an aray in the database. I’m making
a “backpack” for my game. So i’m gana use it in a array.

Mohammad wrote:

Hello, I wanted to know how to make an aray in the database. I’m making
a “backpack” for my game. So i’m gana use it in a array.

Is it possable to use self.scan?

You put it in a seperate table. That’s what relational database design
is all about. Any form of storing an array in a single field of your
database will be a massive drain on performance once your application
reaches moderate useage levels.
-Nathan

On 5/16/06, Mohammad [email protected] wrote:

unknown wrote:

You put it in a seperate table. That’s what relational database design
is all about. Any form of storing an array in a single field of your
database will be a massive drain on performance once your application
reaches moderate useage levels.
-Nathan

But it would only be max 5 so would it realy be harder to read the field
then to go through all of the items read the to_id form and see if it
matches @user.id?

You’re missing a fundamental point about what makes relational
databases useful tools. Go look up “inner join” in a SQL tutorial and
I think you’ll be pleased. Then look up the Rails documentation about
how to accomplish this without having to write a line of SQL.

– James

unknown wrote:

You put it in a seperate table. That’s what relational database design
is all about. Any form of storing an array in a single field of your
database will be a massive drain on performance once your application
reaches moderate useage levels.
-Nathan

But it would only be max 5 so would it realy be harder to read the field
then to go through all of the items read the to_id form and see if it
matches @user.id?

Mohammad wrote:

Hello, I wanted to know how to make an aray in the database. I’m making
a “backpack” for my game. So i’m gana use it in a array.

I left of topic the reason that I wanted to do this is beacuse it would
be easier then having to put in twenty fields for twenty item camcity.
The “inner join” alaways me to select from two tabels I don’t understand
how that would help me. Sorry for being so dumb. Newb to progamming only
three months in rails with two months in ruby. Not really good at it
though.

Cool now how do I call it in the RHTML file? I tried <%
@backpack.each.do |item| -%>
<%= item %>

<% end -%>
That just wrote #
Also, I added “items” database so I hooked that up to backpacks so
backpacks is table is like this id user_id item_id so that will work or
not?

Mohammad wrote:

Cool now how do I call it in the RHTML file? I tried <%
@backpack.each.do |item| -%>
<%= item %>

<% end -%>
That just wrote #
Also, I added “items” database so I hooked that up to backpacks so
backpacks is table is like this id user_id item_id so that will work or
not?

What field do you want to display? Assuming your backpack database has a
field “name”:

<%= item.name %>

-Adam

Don’t worry I didn’t even give enough info. But anyway I got everyting
working

On 5/16/06, Mohammad <ajax_created@ha_im_not.org> wrote:

Mohammad wrote:

Hello, I wanted to know how to make an aray in the database. I’m making
a “backpack” for my game. So i’m gana use it in a array.

I left of topic the reason that I wanted to do this is beacuse it would
be easier then having to put in twenty fields for twenty item camcity.
The “inner join” alaways me to select from two tabels I don’t understand
how that would help me. Sorry for being so dumb. Newb to progamming only
three months in rails with two months in ruby. Not really good at it
though.

It sounds like you want to link backpack items directly to users,
which is fine. If “backpacks” is it’s own table, this will still
work.

Assume two tables: “users” and “backpackitems” each with their own
“id” columns. “backpackitems” also has a “user_id” column, plus
whatever else it needs to house your data.

The point of using an inner join is that you make the database do the
work for you of sorting out which items belong to a given user. You
don’t have to iterate over all of the backpackitems yourself, because
the database is already very good at doing this.

For example, suppose you want to fetch the user with ID=2 and
everything in his backpack. The SQL would look like this:

SELECT * FROM user u
INNER JOIN backpackitems b ON b.user_id = u.id
WHERE u.id = 2;

(“u” and “b” are arbitrary names. Think of them like an alias that
make the SQL easier to read.)

The bonus, however, is that you’re using Rails. You don’t actually
have to know how to use that query at all. If you define the
association between user and backpackitems, then you can let
ActiveRecord do the work for you.

In user.rb:
has_many :backpack_items

In backpack_item.rb:
belongs_to :user

Then in a controller somewhere:
user = User.find(2)
backpack_items = user.backpack_items

Obviously, your names are probably completely different than mine.

– James