Model One to One relationship?

46 views
Skip to first unread message

tanin

unread,
Jun 29, 2011, 6:10:19 AM6/29/11
to Ohm Ruby
I have done:

class Article
attribute :title
reference :article_body, ArticleBody
end

class ArticleBody
attribute :content
reference :article, Article
end


But it does not make much sense because when creating, I have to:

a = Article.new
a.title = "this is title"
a.save

b = ArticleBody.new
b.content = "this is content"
b.article = a
b.save

a.article_body = b
a.save

Does anyone have a better idea?

Carlo Pecchia

unread,
Jun 30, 2011, 2:31:49 AM6/30/11
to ohm-...@googlegroups.com
The article body lines only inside an article, right?
If so... why keep them separated?


2011/6/29 tanin <tan...@gmail.com>:

--
Carlo Pecchia
email: c.pe...@gmail.com
twitter: @carlopecchia

tanin can mend your broken heart

unread,
Jun 30, 2011, 5:30:31 AM6/30/11
to ohm-...@googlegroups.com
I'm afraid that when I query 800 records only for their titles, it would waste time copying content.
--
Tanin Na Nakorn
Chief Technology Officer - Whowish Ltd.
www.whowish.com

Carlo Pecchia

unread,
Jun 30, 2011, 5:50:19 AM6/30/11
to ohm-...@googlegroups.com
Maybe - I repeat "maybe" - you can add an index on :title attribute...

2011/6/30 tanin can mend your broken heart <tan...@gmail.com>:

cyx

unread,
Jun 30, 2011, 6:01:58 AM6/30/11
to Ohm Ruby
Hi Tanin,

What Ohm does is lazy-load all attributes, it's a decision that makes
sense for most cases, since it's so rare that you
actually want to load _every single attribute_ for a model.

Hence the answer is: Don't split the body / title because you're
afraid it will get slow. If you don't access the body, it
won't ever get fetched.

Cyx

On Jun 30, 5:50 pm, Carlo Pecchia <c.pecc...@gmail.com> wrote:
> Maybe - I repeat "maybe" - you can add an index on :title attribute...
>
> 2011/6/30 tanin can mend your broken heart <tani...@gmail.com>:
>
>
>
>
>
>
>
>
>
> > I'm afraid that when I query 800 records only for their titles, it would
> > waste time copying content.
>
> > On Thu, Jun 30, 2011 at 1:31 PM, Carlo Pecchia <c.pecc...@gmail.com> wrote:
>
> >> The article body lines only inside an article, right?
> >> If so... why keep them separated?
>
> >> 2011/6/29 tanin <tani...@gmail.com>:
> >> > I have done:
>
> >> > class Article
> >> >     attribute :title
> >> >     reference :article_body, ArticleBody
> >> > end
>
> >> > class ArticleBody
> >> >     attribute :content
> >> >     reference :article, Article
> >> > end
>
> >> > But it does not make much sense because when creating, I have to:
>
> >> > a = Article.new
> >> > a.title = "this is title"
> >> > a.save
>
> >> > b = ArticleBody.new
> >> > b.content = "this is content"
> >> > b.article = a
> >> > b.save
>
> >> > a.article_body = b
> >> > a.save
>
> >> > Does anyone have a better idea?
>
> >> --
> >> Carlo Pecchia
> >> email: c.pecc...@gmail.com
> >> twitter: @carlopecchia
>
> > --
> > Tanin Na Nakorn
> > Chief Technology Officer - Whowish Ltd.
> >www.whowish.com
>
> --
> Carlo Pecchia
> email: c.pecc...@gmail.com
> twitter: @carlopecchia

tanin can mend your broken heart

unread,
Jun 30, 2011, 11:06:52 AM6/30/11
to ohm-...@googlegroups.com
Hi Cyx,

Will that create another problem?

Suppose I have to load 800 articles per page.

Let's say each article have title, created_date, owner, something1, something2, something3 to be shown on the list page.

Then I will query Redis for 800*6 = 4800 times.
(I test querying Redis for 10,000 on my laptop and it took 5 seconds. Therefore, I think 4800 will take 2.5 seconds which is too much.)

But.. I have done my experiment with Ohm by creating a class with 10 attributes.

I load 800 of them and use every attributes. It took only 0.05 seconds.

Right now I'm confused about lazy loading because it seems to me that it does not use lazy loading.

Tanin

cyx

unread,
Jun 30, 2011, 1:34:59 PM6/30/11
to Ohm Ruby
Hi Tanin,

It would help me to see a code (gist pls) of the two cases you are
talking about.

But yes, I'm pretty sure it does lazy loading, because I've built
custom code before to force HMGET
on a case by case basis.

cyx

On Jun 30, 11:06 pm, tanin can mend your broken heart

Charles Martin

unread,
Jun 30, 2011, 1:43:00 PM6/30/11
to ohm-...@googlegroups.com
I posted an example of this to the board a few weeks back
The idea is to create base classes that automatically create the references

Please let me know if you have questions

tanin

unread,
Jul 6, 2011, 11:59:29 AM7/6/11
to Ohm Ruby
Hi Cyx,

Here is the code.

require 'rubygems'
require 'redis'
require 'ohm'

class Person < Ohm::Model
attribute :name0
attribute :name1
attribute :name2
attribute :name3
attribute :name4
attribute :name5
attribute :name6
attribute :name7
attribute :name8
attribute :name9
end


start_time = Time.now.to_f


(1..800).each { |s|
p = Person[s]
x = p.name0 + " " + p.name1 + " " + p.name2 + " " + p.name3 + " " +
p.name4 + " " + p.name5 + " " + p.name6 + " " + p.name7 + " " +
p.name8 + " " + p.name9
#x = p.name0
}

puts Time.now.to_f - start_time


---------------------------

With this code, on my laptop, it runs for 2.00 seconds.

But if I use the line x = p.name0 instead, it takes only 0.5 seconds.

PS. My laptop is a slow one.

cyx

unread,
Jul 6, 2011, 2:47:35 PM7/6/11
to Ohm Ruby
Hi tanin,

Makes sense. If you load all attributes, that would result in an HGET
for each of the attributes.

It takes less time if you only load name0 because it results in less
HGET.

Hope this makes sense.
Reply all
Reply to author
Forward
0 new messages