sql - multiple joins using activerecord in rails -


I am creating a small Twitter style microblogging service where users can follow other users and send their messages Can get the feed

I have the following models:

  Follow square & lt; ActiveRecord :: Base is_to: follower, class_name = & gt; "User" related_to: follow-up,: class_name = & gt; "User" end class users & lt; ActiveRecord :: Base has has_many: as follows,: foreign_key = & gt; 'Follower_id',: class_name = & gt; Follow 'is' _Maya: Followers ,: By: = & gt; : The following is_main: the following ,: foreign_key => 'Follow-up_id' ,: class_name = & gt; 'Follow' is_maya: followees, through => : Has been followed: message and class message & lt; ActiveRecord :: Base is_to: user end  

To get a feed for the current user, I want to do the following SQL query:

  SELECT * Users join after joining FROM, where messages are included Faller_ID = Current_User.Id and as follows Followee_id = users.id and users.id = messages.user_id;  

What is the right activation method to do this?

Not sure what you are looking for, but here's my suggestion:

< P> I think that you have other objectives to follow, otherwise I do not see the purpose of this.

"Right way" (i.e. my completely subjective ways) To do this, there will really be something like this:

  class users & lt; ActiveRecord :: Base has_and_belongs_to_many: Followers,: foreign_key = & gt; 'Followed_id',: class_name = & gt; 'User' ,: association_foreign_key = & gt; 'Follower_id',: include = & gt; [: Message] has_and_belongs_to_many: as follows: foreign_key = & gt; 'Follower_id',: class_name = & gt; 'User' ,: association_foreign_key = & gt; 'Follow_id' has_many: message and class message & lt; ActiveRecord :: Base is_to: User End  

Then create the following table:

  create_table: users_users ,: id = & gt; Do false T | T.integer: followed_id t.integer: follower_id end  

and you are setting up:

  follow-up = User.find: first follower = User Find: last time chase Author & lt; & Lt; Followed the follower.First.First.Follow the messages.First.First.Folter.First.Commission # etc ...  

But whatever I make it, all of you All followers want to show the message at the same time.

It should be possible by adding

  has_and_belongs_to_many: followed_messages,: foreign_key = & gt; 'Follower_id',: class_name = & gt; 'Message',: association_foreign_key = & gt; For the  user  class 'permissible_id'  

, but I do not know how this would be correct. It may be possible to get acquainted with an association extension but I can not really give any examples. By changing

Update: : class_name, this will add it to Message.id , it does not think about it like this Will not be perfect

Then the only "good" option is to go into the first example like the user class. I can see that only other options are either association extensions (which I can not give you an example) or perhaps using the finder statement.

  is_my: follow_messes ,: class_name = & gt; 'Message',: finder_sql = & gt; * Select * from messages where user_id (select follow_id from users_users where follower_id = # {id}) ' 

You probably have to customize that SQL statement to do everything , But at least you should get the picture


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -