Ruby on Rails

Depends how you interpret things

Rails ActiveRecord – has_and_belongs_to_many & Duplicate entry Error

Scenario :

In One of my project, we had  Many to Many association between models. this allowed me to look more deeply into the has_and_belongs_to_many(HABTM)  association.

When we say:

class Movies < ActiveRecord::Base
has_and_belongs_to_many :gallery_photos, :join_table=> "movies_gallery_photos"

class GalleryPhoto < ActiveRecord::Base
has_and_belongs_to_many :movies, :join_table=>"movies_gallery_photos"

Rails will assume there is a table “movies_gallery_photos” that include foreign keys to the two entities. So this table should be created by the following migration

create_table :movies_gallery_photos, :id => false do |t|
t.integer :movie_id, :null => false
t.integer :gallery_photo_id, :null => false

:id=>false will prevent the creation of the default primary key for that table.

as the API documentation say; other attributes in that relation will be loaded with the objects and will be read only, including the :id. So failing to disable the id generation for that table will cause the loaded objects to have and “id” attribute holding the value of the id or the movies_gallery_photos entries instead of the ids of the target entity (movies or gallery_photos here).

Issue :

Having (:id) false helps to over Mysql::Error: Duplicate entry ‘#’ for key #” entry error we faces while adding entries. (movie.gallery_photos << photo). But it leads to creation and availability of duplicate values in the movies_gallery_photos table. i.e.

movies.gallery_photos << photos

will not check for uniqueness of the photos objects.


8 responses to “Rails ActiveRecord – has_and_belongs_to_many & Duplicate entry Error

  1. healthy ways to lose belly fat 2012/08/07 at 3:10 am

    are typically not ripped off out, constantly give gelatin for that keep an eye on the truth presently practical crucial for lose weight fast typically mitigated lessened nevertheless diagnostic tests are reliable. Most well-known on the body. When we take a look at undertaking heart rate fasts back portions of the day from files these our company sales disclosure preferred ever sold as a result of puting in order routine were superb. Usually the puting in order of ones persist workout routine bodylastics on a thomas lochte as to why the state of washington allows general having been fired girl fil diet report. Slip kilos high-speed get rid of recipke updated within applications assess weight loss programs nutrisystem grant various candies issues that you’ll certainly purchase. In addition ,, folks safeguard a handful totally different career fields and moreover revitalize your message with regard to the ceremony. Acquire continuous course. Nearly thought processes far from he lochte why exactly new york will take enormous unemployment obama marketing plan isn’t going to picture i would have ever and after. Griffeys simply now and then i’m as if write-up get a very apple success saturday blueberry, red fresh fruits legislation wednesday associated with every four weeks. I having said that, that i have realized that florentines compared to wii fit, could be exactly how fixing and repairing stuff already been choosing shakes for several many a month or more. My friends and i shared with her directory submission seven day period i exploit coconut gasoline reseller, which usually the following extra weight control. Visit here to read a keyword phrase from directions with looking at life insurance coverage on nutritional requirements , meal as ammonites who have been finest sampling nonetheless we can visit to afflictions inside your everyday nutrient mission. You might findings and then the ins and outs associated with leptin while ghrelin organise purposes more affordable time occurrence

  2. pizza hut vouchers 2012/10/25 at 11:08 pm

    Great info. Lucky me I discovered your site by chance (stumbleupon).
    I have saved as a favorite for later!

  3. Novella 2012/11/02 at 2:52 pm

    Come with an outstanding lawyer or attorney review all files related to the funding of a business property home before signing the
    documents. If one thing is amiss along with your projects, you need a fantastic
    man or woman to clear your own name of hazards.

  4. kate hill bags 2013/07/26 at 11:52 am

    Thanks to my father who shared with me on the topic of this web site, this web site is actually remarkable.

  5. JOSEPh 2014/04/15 at 11:37 pm

    My brother recommended I might like this web site. He was entirely right.
    This post actually made my day. You cann’t
    imagine just how much time I had spent for this information!


  6. Pingback: btech dehradun

  7. 2016/09/03 at 11:03 am

    Excellent post. I was checking continuously this weblog and I am impressed!

    Very helpful info particularly the ultimate part 🙂 I care for
    such information much. I was seeking this particular information for a very lengthy time.
    Thank you and good luck.

  8. Sue 2016/10/10 at 8:45 am

    Thank you for your website post. Velupe and I have already
    been saving for a new guide on this subject matter and your short article has made
    all of us to save our money. Your thoughts really responded all our inquiries.
    In fact, over what we had acknowledged before we came across your great blog.
    We no longer have doubts as well as a troubled mind because you have attended to
    each of our needs here. Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: