Forum: Ruby on Rails Array in database?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mohammad (Guest)
on 2006-05-16 19:56
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 (Guest)
on 2006-05-16 22:33
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?
unknown (Guest)
on 2006-05-16 22:53
(Received via mailing list)
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
Mohammad (Guest)
on 2006-05-16 23:24
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?
James L. (Guest)
on 2006-05-16 23:32
(Received via mailing list)
On 5/16/06, Mohammad <removed_email_address@domain.invalid> 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
Mohammad (Guest)
on 2006-05-16 23:58
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.
James L. (Guest)
on 2006-05-17 00:17
(Received via mailing list)
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
Mohammad (Guest)
on 2006-05-17 01:19
Cool now how do I call it in the RHTML file? I tried <%
@backpack.each.do |item| -%>
<%= item %><br>
<% 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?
Adam B. (Guest)
on 2006-05-17 04:58
Mohammad wrote:
> Cool now how do I call it in the RHTML file? I tried <%
> @backpack.each.do |item| -%>
> <%= item %><br>
> <% 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
Mohammad (Guest)
on 2006-05-17 05:11
Don't worry I didn't even give enough info. But anyway I got everyting
working
This topic is locked and can not be replied to.