开发者

Is it possible to make a relationship between an ActiveRecord model and a plugin's model?

开发者 https://www.devze.com 2023-01-03 19:32 出处:网络
I\'m using ruby on rails 2.3.8, and I\'ve installed the acts_as_taggable_on plugin for tagging announcements and everything was fine开发者_如何学Go until I discovered I had no easy and neat way of get

I'm using ruby on rails 2.3.8, and I've installed the acts_as_taggable_on plugin for tagging announcements and everything was fine开发者_如何学Go until I discovered I had no easy and neat way of getting all the announcements tagged with a certain id(the only method I found was Tag.tagged_with(tag_name)).

So, I just wanted to relate the Tag model of the plugin, to a Tagging model I created (of ActiveRecord), representing the taggings table that is included within the plugin.

Is it possible to do that?


It should be possible, if they are both ActiveRecord models.

To define the relationship in the plugin's model you'll have to do some metaprogramming somewhere in a library. You can stick it at the end of your environment.rb file at first to experiment.

ModelName.class_eval do
  has_many :announcements
end

That in particular may or may not work but that's the basic idea.

If the metaprogramming proves to be too complicated, you can always just do (making assumptions about the schema here, I haven't used AAT in a few year):

Taggings.find_by_tag_id_and_user_id(@tag.id, @user.id, :include => :user ).map{ |t| t.user) }

0

精彩评论

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