Returns records from 2 tables using mysql and rails 3

need help with this:
 @follows = Follow.where("admin_user_id = ?", session[:id])
This returns entries from the Follow table, which has the following coloumns: admin_user_id ,: assign_id. Now I would like to do this:

@assignments = Assignment.where("id = ?", @follows.assignment_id)

The Asssignment table has the following columns: id ,: name. I tried "@ follow.assignment_id" to replace every identifier that I can then use in the view, like

<% @assignment.each do |assignment| %>
     <%= assignment.name %>
<% end %>

Thanks in advance for your help.

+3
source share
2 answers

- : Follow.where(...) , , @follows. , , , , , ( ):

@follows = Follow.where("admin_user_id = ?", session[:id]).all

, @follows Follow. ( Follow), assign_id. :

@assignments = Assignment.where(:id => @follows.map{|f| f.assignment_id}).all

, .map , , . , SQL- :

select * from assignments where id IN (1, 2, 3)

1, 2 3 .

+3

:

class Follow < ActiveRecord::Base
  belongs_to :assignment
end

class Assignments < ActiveRecord::Base
  has_many :follows
end

@assignments = []
@follows.collect {|f| @assignments << f.assignment}

:

This is the error i get:undefined method `assignment_id' for #<ActiveRecord::Relation:0xb6d8104c>

, sql assign_id

+1

All Articles