Hi I''m trying to create a many to many relationship between two models. A User can be associated with many Incidents and vice versa. class User < ActiveRecord::Base include ActiveModel::ForbiddenAttributesProtection has_many :incident_participants, foreign_key: "participant_id" has_many :participated_incidents, through: :incident_participants end class Incident < ActiveRecord::Base include ActiveModel::ForbiddenAttributesProtection has_many :incident_participants, foreign_key: "participated_incident_id" has_many :participants, through: :incident_participants end The join table: class IncidentParticipant < ActiveRecord::Base include ActiveModel::ForbiddenAttributesProtection t.belongs_to :participant, class_name: "User" t.belongs_to :participated_incident, class_name: "Incident" end Table for IncidentParticipants create_table "incident_participants", :force => true do |t| t.integer "participant_id" t.integer "participated_incident_id" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end So, why doesn''t rails get this relationship? When I try to do @incident.participants in my view I get this error: "Could not find the source association(s) :participant or :participants in model IncidentParticipant. Try ''has_many :participants, :through => :incident_participants, :source => <name>''. Is it one of ?" Any ideas? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9luWgsiDPrIJ. For more options, visit https://groups.google.com/groups/opt_out.
Found the error... I have written t.belongs in one class instead of belongs_to.... Tired... Den fredagen den 8:e februari 2013 kl. 15:28:16 UTC+1 skrev Linus Pettersson:> > Hi > > I''m trying to create a many to many relationship between two models. > > A User can be associated with many Incidents and vice versa. > > class User < ActiveRecord::Base > include ActiveModel::ForbiddenAttributesProtection > > has_many :incident_participants, foreign_key: "participant_id" > has_many :participated_incidents, through: :incident_participants > > end > > > class Incident < ActiveRecord::Base > include ActiveModel::ForbiddenAttributesProtection > > has_many :incident_participants, foreign_key: "participated_incident_id" > has_many :participants, through: :incident_participants > > end > > > The join table: > > class IncidentParticipant < ActiveRecord::Base > include ActiveModel::ForbiddenAttributesProtection > > t.belongs_to :participant, class_name: "User" > t.belongs_to :participated_incident, class_name: "Incident" > end > > Table for IncidentParticipants > create_table "incident_participants", :force => true do |t| > t.integer "participant_id" > t.integer "participated_incident_id" > t.datetime "created_at", :null => false > t.datetime "updated_at", :null => false > end > > > So, why doesn''t rails get this relationship? When I try to do > @incident.participants in my view I get this error: > "Could not find the source association(s) :participant or :participants in > model IncidentParticipant. Try ''has_many :participants, :through => > :incident_participants, :source => <name>''. Is it one of ?" > > > Any ideas? >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/hnEaL1m2hX8J. For more options, visit https://groups.google.com/groups/opt_out.