如何通过rails中的关联获取详情?
How to get details through association in rails?
我在rails中有四个模型;位置、路径、特征和特征类型。但是我正在努力建立一些协会。
- 一个地图项有一个位置和一种地图项类型
- 一条路径有 2 个位置,从和到
从功能我可以运行
- Feature.first.location - 并获取它适用的位置
- Feature.featuretype - 并获取与之相关的要素类型。
从特征类型我可以运行
- Featuretype.first.features - 它 return 是使用此特征类型的所有特征。
从位置我可以运行
- Location.first.features - 它 return 链接到此位置的所有功能。
我希望能够运行
- Location.first.paths - 和 return 使用该位置的所有路径。
- Path.first.locations - 和 return 路径中的两个位置。
如有任何帮助,我们将不胜感激。
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
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
中创建一个方法,该方法 returns 将两个位置作为数组
def locations
[location_from, location_to]
end
我在rails中有四个模型;位置、路径、特征和特征类型。但是我正在努力建立一些协会。
- 一个地图项有一个位置和一种地图项类型
- 一条路径有 2 个位置,从和到
从功能我可以运行
- Feature.first.location - 并获取它适用的位置
- Feature.featuretype - 并获取与之相关的要素类型。
从特征类型我可以运行
- Featuretype.first.features - 它 return 是使用此特征类型的所有特征。
从位置我可以运行
- Location.first.features - 它 return 链接到此位置的所有功能。
我希望能够运行
- Location.first.paths - 和 return 使用该位置的所有路径。
- Path.first.locations - 和 return 路径中的两个位置。
如有任何帮助,我们将不胜感激。
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
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
中创建一个方法,该方法 returns 将两个位置作为数组
def locations
[location_from, location_to]
end