0

私は@postsという投稿の配列を持っています。ポストモデルhas_many:feelings:through =>:feelingships。Rails 3:has_many:through関係のデータベース呼び出し

投稿の配列を特定の感情の投稿に限定するにはどうすればよいですか?

私は以下のコードを試してみましたが、それは:(

@specific_feeling_posts = @posts.feeling.where(:feeling => "happy") 

モデルに動作するはずです

class Post < ActiveRecord::Base 
    has_many :feelingships 
    has_many :feelings, :through => :feelingships 
    belongs_to :user 
end 

class Feeling < ActiveRecord::Base 
    has_many :feelingships 
    has_many :posts, :through => :feelingships 
end 

class Feelingship < ActiveRecord::Base 
    belongs_to :post 
    belongs_to :feeling 
end 

答えて

1
@happy_posts = Post.joins(:feelings).where("feelings.title = ?", "happy") 

を動作しません。

0
@specific_feelings_post=Post.join(:feelings).where("feelings.title= ?","specific_feeling") 

その、同じラインをレンガが上に書いたように、疑問符SQLインジェクションを避けることです。 つまり、データベースのセキュリティは、RailsのActiveRecordによって処理されます。これにより、適切にエスケープされたSQlを作成し、SQLインジェクションから免除されます。

関連する問題