开发者

Filter on "include" - Rails 3

开发者 https://www.devze.com 2023-02-18 07:57 出处:网络
these are my associations: class Activity <开发者_如何学运维; ActiveRecord::Base has_many :infos, :dependent => :destroy

these are my associations:

class Activity <开发者_如何学运维; ActiveRecord::Base

  has_many :infos, :dependent => :destroy
  has_many :events, :dependent => :destroy
  accepts_nested_attributes_for :infos

end

class Event < ActiveRecord::Base

  belongs_to :activity
  has_many :infos, :through => :activity

end

class Info < ActiveRecord::Base

  has_one :language
  belongs_to :activity

end

Now i can get an XML with all the events and their infos using:

@events = Event.all

respond_to do |format|
  format.xml  { render :xml => @events.to_xml(:include => [:infos ]) }
end

The problem is that i get the infos from all the language.

Is it possible to use a filter (as "where info.language.id==1"), so that only the language1 infos are displayed in the XML for each event?

Thanks

UPDATE :

Hi Mike, thanks for your answer.

Unfortunately i'm getting this error:

undefined method `eq' for nil:NilClass Rails.root: /Users/abramo/village

Application Trace | Framework Trace | Full Trace app/controllers/events_controller.rb:29:in block (2 levels) in locale' app/controllers/events_controller.rb:28:in locale'

and lines 28,29 are the last line of my locale method:

  def locale
    @events = Event.joins(:infos => :language).where("languages.id = 2")

    respond_to do |format|
      format.xml  { render :xml => @events.to_xml(:include => [:infos ]) }
    end
  end

I really don't understand... what object is Nil?


Event.joins(:infos => :language).where("languages.id = 1")
0

精彩评论

暂无评论...
验证码 换一张
取 消