- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有这个模型和关联多对多:通过:
class RailwayStation < ActiveRecord::Base
has_many :railway_stations_routes
has_many :routes, through: :railway_stations_routes
end
class Route < ActiveRecord::Base
has_many :railway_stations_routes
has_many :railway_stations, through: :railway_stations_routes
end
class RailwayStationsRoute < ActiveRecord::Base
belongs_to :railway_station
belongs_to :route
end
我添加了列 st_index
:
add_column :railway_stations_routes, :st_index, :integer
对于 route 的索引站,但我不明白如何从路线 View 表单中更改它:
ul
- @route.railway_stations.each do |railway_station|
li = railway_station.title
= ????
最佳答案
首先,您需要更正模型和表格的命名方案,以便它们遵循 Rails 约定。
从命令行运行:
$ rails g migration RenameRailwayStationsRoute
并编辑“db/migrations”中的迁移以读取:
class RenameRailwayStationsRoute < ActiveRecord:Migration
def change
rename_table :railway_stations_route, :railway_station_routes
end
end
运行迁移
$ rake db:migrate
重命名模型文件:
$ cd app/models
$ mv railway_stations_route.rb railway_station_route.rb
或者如果你正在使用 GIT
$ git mv railway_stations_route.rb railway_station_route.rb
编辑您的模型以使用正确的命名:
class RailwayStation < ActiveRecord::Base
has_many :railway_station_routes
has_many :routes, through: :railway_station_routes
end
class RailwayStationRoute < ActiveRecord::Base
belongs_to :railway_station
belongs_to :route
end
class Route < ActiveRecord::Base
has_many :railway_station_routes
has_many :railway_stations, through: :railway_station_routes
end
最简单的方法是使用 simple_form
gem 。按照说明进行操作(并记住重新启动 Rails 服务器)后,添加表单:
<%= simple_form_for(@route) do |f| %>
<%= f.association :stations, collection: Station.all, name_method: :title %>
<% end %>
如果没有简单的形式,你可以这样做:
<%= form_for(@route) do |f| %>
<%= f.collection_check_boxes(:stations_ids, Station.all, :id, :title) do |b|
b.label { b.checkbox checked: @route.stations_ids.include?(object.id) }
end %>
<% end %>
不幸的是,没有直接的方法可以从多对多关系的一端访问连接模型的属性。
这是因为 Rails 不会跟踪模型的加载位置,至少不是以您想要的方式加载。 (确实如此 - 但它非常复杂)。
解决这个问题的一种方法是:
class RoutesController < ApplicationController
def show
@route = Route.eager_load(railway_station_routes: :railway_station).find(params[:id])
end
end
我们使用 eager_load
加入两个模型,这样 rails 运行一个数据库查询并加载 railway_station_routes 和 railway_station。
然后您将遍历连接模型而不是站点:
ul
- @route.railway_station_routes.each do |rs|
li
# note that you cannot both use = and have a body in HAML/Slim
p = "index: #{ rs.st_index }"
p = rs.railway_station.title
关于ruby-on-rails - 访问 has_many 中的连接模型属性 : though relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047083/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!