0

Suppose i have a Cat domain as shown below.

  class Cat{

     String name
     Integer age

    }

Now i want to add a new feature that will assign tags to cats.

Now i see two options to achieve this.

Option 1

add a property to the Cat domain

class Cat{

 String name
 Integer age
 String tag

}

Option 2

Create a new table

class CatTagAssignment{

 Cat cat
 String tag

 static mapping = {

        id composite: ['cat', 'tag']

    }

}

I am trying to understand which is a better approach or which one should i prefer when i want to add a new feature? I appreciate it if you can offer pros and cons of both approach and whether one approach is always preferable to the other. I appreciate your help! Thanks!

kofhearts
  • 3,607
  • 8
  • 46
  • 79

2 Answers2

1

Make a tag model (define your tags), and a cattag model (relate tags to cats). Should you ever want to be able to 'tag' something else, it's a simple join table to the new model.

Which to prefer? Ignore code and frameworks and think about what you are modeling.

Tags as a separate entity are more reusable, and a cat remains a cat (is a tag really an attribute of a cat?). Good models should be transferable across database, framework, language.

railsdog
  • 1,503
  • 10
  • 10
1

Use the simplest model you can get. Don't use the extra tables for the sake of "object orientation" alone, unless there's no other option.

I would pick 1) as it's the fastest possible way to load objects, although the tag lookup can get a bit tricky (like query)

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • one cons i notice is that in future adding more and more columns to a table will make the table size really big. can we opt for option 2 in such cases? Thanks! – kofhearts Nov 24 '16 at 05:25