> On Feb 2, 2018, at 9:07 PM, fugee ohu <fugee279@gmail.com> wrote:
>
>
>
> On Friday, February 2, 2018 at 5:01:05 PM UTC-5, Colin Law wrote:
> On 2 February 2018 at 19:39, fugee ohu <fuge...@gmail.com> wrote:
>
> So if I'm using the second approach the has_many_through then how do i create a new record?
>
> Start by reading the rails guide on active record relations, that shows you how to do that sort of thing.
>
> Colin
>
>
>
> This code `@person.pictures << @picture` causes an error
>
> Could not find the association :people_pictures in model Person
>
>
> class PeoplePictures < ApplicationRecord
> belongs_to :person
> belongs_to :picture
>
> class Person < ApplicationRecord
> has_many :pictures, through: :people_pictures
>
> class Picture < ApplicationRecord
> has_many :people, through: :people_pictures
>
>
If you have read the guide, as Colin recommended, really read every line of it, you would have seen this:
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
See how each has_many is described with two, separate has_many relationships? One goes to the "join object" and another to the "joined object". You are missing the association to the 'join object', which in your relationship would be:
has_many :people_pictures
Walter
> --
> 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@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/81d55ecb-6bda-41b1-a2c5-2d5f868affed%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6FD91DC4-9382-440B-9F6A-C401A4F4D37D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment