【问题标题】:How to get details through association in rails?如何通过rails中的关联获取详细信息?
【发布时间】:2016-08-15 09:40:46
【问题描述】:

我在 Rails 中有四个模型;位置、路径、特征和特征类型。但我正在努力建立一些协会。

  • 要素具有一个位置和一种要素类型
  • 一条路径有 2 个位置,起点和终点

从我可以运行的功能

  • Feature.first.location - 并获取它适用的位置
  • Feature.featuretype - 并获取与其相关的特征类型。

我可以从 Featuretype 运行

  • Featuretype.first.features - 它返回使用此特征类型的所有特征。

从我可以运行的位置

  • Lo​​cation.first.features - 它返回链接到该位置的所有要素。

我希望能够跑步

  • Lo​​cation.first.paths - 并返回使用该位置的所有路径。
  • Path.first.locations - 并返回路径中的两个位置。

任何帮助将不胜感激。

class Location < ActiveRecord::Base
    has_many :features
end

class Path < ActiveRecord::Base
    belongs_to :LocationFrom, class_name: 'Location', foreign_key: 'from'
    belongs_to :LocationTo, class_name: 'Location', foreign_key: 'to'
end

class Feature < ActiveRecord::Base
    belongs_to :featuretype
    belongs_to :location
end

class Featuretype < ActiveRecord::Base
    has_many :feature 
end

如果有帮助,这是我的数据库架构

ActiveRecord::Schema.define(version: 20160813164514) do

  create_table "features", force: :cascade do |t|
    t.integer  "featuretype_id"
    t.string   "featurename"
    t.string   "featuredescription"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "location_id"
  end

  create_table "featuretypes", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "locations", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.string   "latitude"
    t.string   "longitude"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "paths", force: :cascade do |t|
    t.integer  "from"
    t.integer  "to"
    t.integer  "distance"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean  "directed"
  end

end

【问题讨论】:

    标签: ruby-on-rails associations rails-activerecord


    【解决方案1】:
    class Featuretype < ActiveRecord::Base
      has_many :feature # Should be :features, (notice the pluralization)
    end
    

    重命名关系。命名约定是蛇形大小写。

    class Path < ActiveRecord::Base
      belongs_to :location_from, class_name: 'Location', foreign_key: 'from'
      belongs_to :location_to, class_name: 'Location', foreign_key: 'to'
    end
    

    在Location 中创建一个查询Path 所需行的方法

    def paths
      Path.where "from = :id OR to = :id", id: id
    end
    

    在Path 中创建一个将两个位置作为数组返回的方法

    def locations
      [location_from, location_to]
    end
    

    【讨论】:

    • 这些方法是要添加到模型还是控制器中?
    • 我无法让这些方法发挥作用。我不得不改用[Location.find(from),Location.find(to)] 和[Path.where(from: id),Path.where(to: id)],它们都产生了我想要的结果。
    • 运行paths方法时遇到了什么错误?
    • 我收到ActiveRecord::StatementInvalid: SQLite3::SQLException: near "from": syntax error: SELECT "paths".* FROM "paths" WHERE (from = 4 OR to = 4)
    • @Conor 尝试删除 = "from=:id OR to=:id", id: id 周围的空格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2021-03-23
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多