ruby on rails 3 - Active Record Querying belong_to -



ruby on rails 3 - Active Record Querying belong_to -

i have 2 next models associated:

class post < activerecord::base belongs_to :language end class language < activerecord::base has_many :posts end

from view have link filter posts language:

<div id="english"><%= link_to "english", {:controller => 'posts', :action => 'search_result', :language => "english"} %></div>

the model language has variable name:string 1 using create active record query.

the uncertainty have how can create query post controller retrieve right posts has field: language.name == "english".

i tried this:

@posts = post.all(:conditions => ["language.name = ?", params[:language]])

and this:

@posts = post.where(:language.name => params[:language])

hope have explained issue, quite newbie yet. ah! know improve in case use: "all" or "where" ??. lot in advance.

you need join: http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables

if have understood models/database construction correctly, activerecord phone call should looks like:

post.joins(:language).where('languages.name' => params[:language])

hope helps.

ps. where phone call preferred method these days.

ruby-on-rails-3 activerecord associations

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -