Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

This is a discussion on Book,Movie,Album many-to-many Pictures, How to implement therelationship about it? within the Object forums in Theory and Concepts category; Hi,All ! There had a design problem really bother me. I had four models Book,Movice,Album and Picture Book,Movice and Album each of them could have many of pictures. now I had three approches to build relationship bewteen them. 1st,I add 'resource_type' and 'resource_id' attributes to picture model, so if I want to get all covers about the book which id is 6,I can specify resource_type equals 'book' and 'resource_id' equals 6. 2nd,I add three joining table:BookCovers, MoviePosters and AlbumCovers. For this, I also need to add three corresponding models and controllers,but I don't want to. 3rd,I use three same structure(also ...

Go Back   Application Development Forum > Theory and Concepts > Object

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-12-2008, 10:53 PM
Vince
Guest
 
Default Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Hi,All ! There had a design problem really bother me.

I had four models
Book,Movice,Album and Picture
Book,Movice and Album each of them could have many of pictures.

now I had three approches to build relationship bewteen them.

1st,I add 'resource_type' and 'resource_id' attributes to picture
model, so if I want to get all covers about the book which id is 6,I
can specify resource_type equals 'book' and 'resource_id' equals 6.

2nd,I add three joining table:BookCovers, MoviePosters and
AlbumCovers. For this, I also need to add three corresponding models
and controllers,but I don't want to.

3rd,I use three same structure(also named BookCovers,MoviePosters and
AlbumCovers,but the are not joining table,the structure of them are
more like Pictures table) tables instead of Pictures table,like the
2nd approch, I also need to add three corresponding models and
controllers

which way is better or no one is suitable? btw, I work on rails
framework, if also need to take it into consideration.

At last, sorry about my English, it's not my native language.so if any
thing is not clear enough,please tell me.
Reply With Quote
  #2  
Old 08-12-2008, 11:47 PM
Phlip
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Vince wrote:

> I had four models
> Book,Movice,Album and Picture
> Book,Movice and Album each of them could have many of pictures.


Regardless of the various abstract design questions this raises...

....you can get to coding sooner by throwing this plugin at your problem:

http://www.thoughtbot.com/projects/paperclip

It shows how one Picture object can automatically upload, reposit, resize, and
re-purpose batches of images.

I saw a demo, and it made me want to scrap our home-grown system we use at work.
We can't - ours has twice the features - but the demo showed how to do them all
much better.

--
Phlip
Reply With Quote
  #3  
Old 08-13-2008, 12:28 AM
Daniel Pitts
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Vince wrote:
> Hi,All ! There had a design problem really bother me.
>
> I had four models
> Book,Movice,Album and Picture
> Book,Movice and Album each of them could have many of pictures.

Big question: Do they possibly share pictures?

> now I had three approches to build relationship bewteen them.
>
> 1st,I add 'resource_type' and 'resource_id' attributes to picture
> model, so if I want to get all covers about the book which id is 6,I
> can specify resource_type equals 'book' and 'resource_id' equals 6.

This would work, but I don't think its "best" in design.
>
> 2nd,I add three joining table:BookCovers, MoviePosters and
> AlbumCovers. For this, I also need to add three corresponding models
> and controllers,but I don't want to.

This is /probably/ the approach I would use. Why would you have to add
corresponding models and controllers? The relationships should be a part
of the existing modeled objects.
>
> 3rd,I use three same structure(also named BookCovers,MoviePosters and
> AlbumCovers,but the are not joining table,the structure of them are
> more like Pictures table) tables instead of Pictures table,like the
> 2nd approch, I also need to add three corresponding models and
> controllers

Again, I don't see why you would need to add models and controllers.
The model for Book should have a slot called Covers which is Set (or
List) of Pictures. The controller for Book should handle the relationships.
>
> which way is better or no one is suitable? btw, I work on rails
> framework, if also need to take it into consideration.

I'm not familiar with ruby or rails, so I might be wrong about needed
extra models/controllers, but I think you're decomposing your design too
far. In Java, using Hibernate, I wouldn't have a model class for the
relationships, but a "@OneToMany Set<Picture> art" in each of book,
Movie, and Album.
>
> At last, sorry about my English, it's not my native language.so if any
> thing is not clear enough,please tell me.

Seemed pretty clear to me. A few misspelled words, but even native
speakers do that frequently enough :-)

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Reply With Quote
  #4  
Old 08-13-2008, 02:33 AM
Vince
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

@Phlip,thanks,but I use fleximage,it works pretty well;

@Daniel,Thank you for your replay.

> Big question: Do they possibly share pictures?

They don't share pictures

>
> > now I had three approches to build relationship bewteen them.

>
> > 1st,I add 'resource_type' and 'resource_id' attributes to picture
> > model, so if I want to get all covers about the book which id is 6,I
> > can specify resource_type equals 'book' and 'resource_id' equals 6.

>
> This would work, but I don't think its "best" in design.


The same to me.

>
> > 2nd,I add three joining table:BookCovers, MoviePosters and
> > AlbumCovers. For this, I also need to add three corresponding models
> > and controllers,but I don't want to.

>
> This is /probably/ the approach I would use. Why would you have to add
> corresponding models and controllers? The relationships should be a part
> of the existing modeled objects.
> ......


I juse work on rails a few weeks ago,not familiar yet.In
Rails,especially in REST style,I feel too many restricts to make
things not easy when you just start use it.
But everyone knows the first step is the hardest and I know these
restricts end up will bring some good stuff for me.

In Rails, if you want to two model have many-to-many relationship,you
must need two tables for model and a joining table and three
corresponding models.
For example,Book, Cover, BookCover,in many-to-many relationship, they
are look like this:

class BookCover < ActiveRecord::Base
belongs_to :book
belongs_to :cover
end

class Book < ActiveRecord::Base
has_many :book_covers
has_many :covers, :through => :book_covers
end

class Cover < ActiveRecord::Base
has_many :book_covers
has_many :books, :through => :book_covers
end

and each of them need a corresponding controller.So if I must add
these classes, whether it's worth?
Reply With Quote
  #5  
Old 08-13-2008, 10:05 AM
Phlip
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Vince wrote:

> @Phlip,thanks,but I use fleximage,it works pretty well;


I can't compare fleximage, but you should study paperclip's object model. I
certainly will.

--
Phlip
Reply With Quote
  #6  
Old 08-13-2008, 11:45 AM
H. S. Lahman
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Responding to Vince...

I never heard of the "rails framework", so this is just a vanilla OOA/D
response...

> I had four models
> Book,Movice,Album and Picture
> Book,Movice and Album each of them could have many of pictures.


I assume from the post's title that a picture can appear on multiple
Albums, multiple Books, and multiple Movies(?)

>
> now I had three approches to build relationship bewteen them.
>
> 1st,I add 'resource_type' and 'resource_id' attributes to picture
> model, so if I want to get all covers about the book which id is 6,I
> can specify resource_type equals 'book' and 'resource_id' equals 6.
>
> 2nd,I add three joining table:BookCovers, MoviePosters and
> AlbumCovers. For this, I also need to add three corresponding models
> and controllers,but I don't want to.


I assume the pictures appear only on the covers(?)

I assume the covers are unique to the Book, etc.(?)

If all these assumptions are true, the we have:

[Book]
| 1
| describes
|
| R1
|
| 1
[BookCover]
| *
| appears on
|
R2 |------------- [???]
|
| *
[Picture]

with a similar arrangement for Movie and Album vs. Picture. The tricky
part is the association object for the *:* association. Typically this
is resolved at the OOP level by reifying the *:* relationship into two
1:* relationships. For example,

[BookCover]
| *
| maps to
|
| R2A
|
| 1[list]
| 1
|
| R2B
|
| maps from
| *
[Picture]

Thus there are four possible combinations of multiplicities. In the
example, each BookCover has a single, unique List of Picture objects and
each Picture has a single, unique list of BookCover objects. That is, a
List collection object is instantiated for each BookCover AND each
Picture. When either a BookCover or Picture is added/removed that
affects both Lists so the List collections on both sides (R2A and R2B)
need to be updated.

One critical question is whether the navigation of the original *:*
association has to be bidirectional. That is, do you have to access
BookCovers given a Picture AND access Pictures given a BookCover? If the
navigation is always one-way in the problem in hand, one can convert the
original *:* relationship directly to a single 1:* relationship with the
1 on the side from which navigation originates. That's because we
usually only abstract what is important to the problem in hand. So if we
only have to access Pictures from BookCovers, it doesn't matter how many
BookCovers the Picture is actually on.


--
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@pathfindermda.com
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@pathfindermda.com for your copy.
Pathfinder is hiring:
http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH
Reply With Quote
  #7  
Old 08-13-2008, 03:25 PM
Daniel Pitts
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Vince wrote:
> @Phlip,thanks,but I use fleximage,it works pretty well;
>
> @Daniel,Thank you for your replay.
>
>> Big question: Do they possibly share pictures?

> They don't share pictures
>
>>> now I had three approches to build relationship bewteen them.
>>> 1st,I add 'resource_type' and 'resource_id' attributes to picture
>>> model, so if I want to get all covers about the book which id is 6,I
>>> can specify resource_type equals 'book' and 'resource_id' equals 6.

>> This would work, but I don't think its "best" in design.

>
> The same to me.
>
>>> 2nd,I add three joining table:BookCovers, MoviePosters and
>>> AlbumCovers. For this, I also need to add three corresponding models
>>> and controllers,but I don't want to.

>> This is /probably/ the approach I would use. Why would you have to add
>> corresponding models and controllers? The relationships should be a part
>> of the existing modeled objects.
>> ......

>
> I juse work on rails a few weeks ago,not familiar yet.In
> Rails,especially in REST style,I feel too many restricts to make
> things not easy when you just start use it.
> But everyone knows the first step is the hardest and I know these
> restricts end up will bring some good stuff for me.
>
> In Rails, if you want to two model have many-to-many relationship,you
> must need two tables for model and a joining table and three
> corresponding models.
> For example,Book, Cover, BookCover,in many-to-many relationship, they
> are look like this:
>
> class BookCover < ActiveRecord::Base
> belongs_to :book
> belongs_to :cover
> end
>
> class Book < ActiveRecord::Base
> has_many :book_covers
> has_many :covers, :through => :book_covers
> end
>
> class Cover < ActiveRecord::Base
> has_many :book_covers
> has_many :books, :through => :book_covers
> end
>
> and each of them need a corresponding controller.So if I must add
> these classes, whether it's worth?

Again, I don't know ruby so I'm guessing at syntax/semantics, but what
about this structure:

class Book < ActiveRecord::Base
has_many :book_covers
end

class BookCover < ActiveRecord::Base
belongs_to :book
<PICTURE_SPECIFICS_GO_HERE>
end


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Reply With Quote
  #8  
Old 08-13-2008, 10:55 PM
Vince
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

to Phlip,Paperclip is good for attachment,that's what I need
too,thanks again.

to Daniel
class Book < ActiveRecord::Base
has_many :book_covers
end

means Book extends ActiveRecord::Base(a persistence pattern
implementation) and one book instance can have many book covers.
has_many and belongs_to methods are pretty much like some contracts,it
makes the framework to manage the relationship(both in database and
logic models) between objects rather than you do it by yourself.

thanks Lahman, the course of your analysis is so clear.

But still some problem remains which is more about rails than OOD,




Reply With Quote
  #9  
Old 08-14-2008, 06:45 AM
S Perryman
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement the relationship about it?

"Vince" <kaizen.Liu@gmail.com> wrote in message
news:ba11e225-3ee1-4e09-a4e2-3db5805a3f4c@1g2000pre.googlegroups.com...

> Hi,All ! There had a design problem really bother me.


> I had four models
> Book,Movice,Album and Picture
> Book,Movice and Album each of them could have many of pictures.


> now I had three approches to build relationship bewteen them.


The most general model I could think of is :

0..1
Index -------- Description
|
ImageCollection ---------- Image
*

ImageCollection :

+ elements : 1..MAX
+ title : string

IC1 : FORALL e IN Index : e <= elements


Now we can do suitable definitions ...

ImageCollection <I--- BookCover
ImageCollection <I--- Picture
ImageCollection <I--- Movie


BookCover :

BC1 : elements IN [2,4]
BC2 : outside-front = Image.Index(1)
BC3 : outside-back = Image.Index(2)
BC4 : inside-left = Image.Index(3)
BC5 : inside-right = Image.Index(4)


Picture :

P1 : elements = 1
P2 : image = Image.Index(maximum)


Movie (30fps etc) :

* running-time (seconds) 1..N

M1 : elements <= 30 * running-time


Regards,
Steven Perryman


Reply With Quote
  #10  
Old 08-14-2008, 11:00 AM
Daniel Pitts
Guest
 
Default Re: Book,Movie,Album many-to-many Pictures, How to implement therelationship about it?

Vince wrote:
> to Phlip,Paperclip is good for attachment,that's what I need
> too,thanks again.
>
> to Daniel
> class Book < ActiveRecord::Base
> has_many :book_covers
> end
>
> means Book extends ActiveRecord::Base(a persistence pattern
> implementation) and one book instance can have many book covers.
> has_many and belongs_to methods are pretty much like some contracts,it
> makes the framework to manage the relationship(both in database and
> logic models) between objects rather than you do it by yourself.

Isn't that exactly what you were asking for? Letting the framework
manage the relationship, so you don't have to?
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:13 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.