ruby on rails - has_many through self referential association -
I want (as an example) for all posts by friends of has_many
Create association person, such as person & gt; Something like
. has_many: remote_posts
to give me something; Friends & gt; Person & gt; Post
.. Here's how I would go about it
Generate script / model post title: string person_id: integer script / model friendship person_id generated: integer friend_id: integer script / Model Person Name: String
Class person & lt; ActiveRecord :: Base has has_many: The post is has_many: friendship ,: foreign_key = & gt; 'Friend_id' near_main: People ,: through => & gt; : Friendship has_many: remote_posts,: class_name = & gt; 'Post' ,: By: = & gt; : People ,: Source = & gt; : Posts & Class Friendship & lt; ActiveRecord :: Base is_to: Friend #Allo's friend to see that the purpose of friendship is in the final post & lt; ActiveRecord :: Base is_to: End of person
# Build some people and friends ('frank' => ['Bob', 'Phil' ], 'Bob' => ['Phil']}. Make every {| ke, v | v.each {| f | friendship} ((person_id = & gt; Person.find_or_create_by_name (f) .id ,: Friend_id = & gt; person.find_or_create_by_name (k) .id)}} # generate some posts Person.all.each {| p | P.ps.s.create ({: title = & gt; "# {p. Name} posted by}}}}
Now,
person.First.mailspeed # ..works person.first.people # ( Friends) ..works person.first.posts # ..works person.first.remote_posts # ....
... and this tru me .
ActiveRecord :: Statement unauthorized: SQLite3 :: SQLException: There is no such column: People. PERSON_ID: Select "post". Go to "post" on INNER "people" Apart from the foreign key error - it seems the friendship association is not coming out in the drama as well. is. I was thinking that this is
: source = & gt; The reason for this may be << Code> Post
Association will come in twice.
I can write some Finder SQL (and this is the moment I have worked) though I do it this way soon.
How is this any idea to work?
How about:
Friendship class
In the square, add:
has_many: posts ,: through = & gt; : In Person
and person
category, change the remote_post to:
is_my: remote_post,: class_name = & Gt; 'Post' ,: By: = & gt; : Friendship ,: source = & gt; : Person
Comments
Post a Comment