如何在Rails中将参数从一个模型传递到另一个模型

时间:2013-05-13 14:09:47

标签: ruby-on-rails models

您好我有两个型号公司和Feed

company.rb

class Company < ActiveRecord::Base
  attr_accessible :rss_url, :name
  has_many :feeds
end

feed.rb

class Feed < ActiveRecord::Base
  attr_accessible :guid, :name, :published_at, :summary, :url
  after_create { |feed| FeedEntry.update_from_feed(feed.feed_url) }
  belongs_to :company

  def self.update_from_feed(rss_url)   ?????
    feed = Feedzirra::Feed.fetch_and_parse(rss_url)   ?????
    add_entries(feed.entries)
  end
end

两个人如何从公司获取RSS_URL?

1 个答案:

答案 0 :(得分:0)

我不知道add_entries的作用,但如果您想访问rss_url内的Feed公司,则可以使用该关联。在Feed类上定义此实例方法。

def do_something
  url = self.company.rss_url
  puts "This feed belongs to a company with the following rss url: #{url}"
end