Rails:是否可以通过关联定义虚拟属性?

时间:2014-09-15 22:27:56

标签: ruby-on-rails ruby-on-rails-4 pg virtual-attribute pg-search

我有两种模式:

SkillnamesSkills

User可以在skillname中创建skillskills model我有参考" skill_id"这意味着当我Skillname.where(:id => @skill.skill_id)时,它会给我一个名字。

现在我对某些事情感到非常困惑。我使用PG_Search并使用associated_against创建了搜索,现在我可以使用skill_id搜索用户的技能(这不是那么聪明) 我想知道我是否可以在技能模型中创建这样的东西:

虚拟属性:

  def skillname=(skillname)
    Skill.where(:skill_id => Skillname.where(:name => skillname).first.id)
  end

更新:添加模型:

class Skill < ActiveRecord::Base
  include PgSearch
    attr_accessible :user_id, :skill_id
    belongs_to :user
end

class User < ActiveRecord::Base

  include PgSearch
  pg_search_scope :search, against: [:first_name, :last_name,:country],
                  :associated_against => {
                      :skills => :name,
                    }

end

class Skillname < ActiveRecord::Base
  include PgSearch
    belongs_to :user
    attr_accessible :user_id, :description, :name



end

0 个答案:

没有答案