lundi 18 mai 2015

How can i Create many to many relationship in spree e commerce?

I have using Many-To-Many Relationship in Spree eCommerce.

I have User Three table Listed Below.

1) Address Table of Spree eCommerce.

 module Spree

   class Address < Spree::Base

     has_many :relation_addresses
     has_many :relations,:through => :relation_addresses

   end

 end

2) Relation Table Custom Created in Spree eCommerce.

   class Relation < ActiveRecord::Base

     has_many :relation_addresses
     has_many :addresses, :class_name => "Spree::Address",:through => :relation_addresses

    end

3) RelationAddress Table Custom Created in Spree eCommerce.

 class RelationAddress < ActiveRecord::Base

    belongs_to :relation
    belongs_to :address, :class => "Spree::Address"

 end

I Create one Record of Relation.

@relation = Relation.create(:name=>”xxxx”,:relation_type=>”xxxx”)

I Create one Record of Address.

@address= Spree::Address.create(:address => “xxxxxxxxx”,:area => “xxxxx”,:city => “xxxxx”)

I create Relation.

@address.relations << @relation

I have getting with Address objct to Relation But can not get Relation object to address.

Example:

@address.relations

o/p:

Relation id: 3, relation_type: "xxxxxx", relative_name: "xxxxxx", user_id: 6, created_at: "2015-05-18 09:46:12", updated_at: "2015-05-18 09:46:12"

but @relation.addresses not getting but gives in error.

NoMethodError: undefined method `primary_key' for "Spree::Address":String

Aucun commentaire:

Enregistrer un commentaire