Ruby on rails 在select_标记中按字母顺序排序,在rails中连接表
我有一行代码:Ruby on rails 在select_标记中按字母顺序排序,在rails中连接表,ruby-on-rails,forms,activerecord,html-select,Ruby On Rails,Forms,Activerecord,Html Select,我有一行代码: <%= select_tag :friendship_id, options_from_collection_for_select(current_user.friendships, "id", "name", selected = nil) %> 在友谊控制器中 该协会的工作,因为我看到了网页上的名单 我想: 按字母顺序排列 在列表顶部添加“选择朋友…” 我还没有找到任何东西。 对于问题2,我添加selected=nil无效 提前谢谢你的帮助 编辑(回答): 我
<%= select_tag :friendship_id, options_from_collection_for_select(current_user.friendships, "id", "name", selected = nil) %>
在友谊控制器中
该协会的工作,因为我看到了网页上的名单
我想:
<%= select_tag :friend_id, options_from_collection_for_select(current_user.friends, "id", "name"), :prompt => 'Select a friend...', :id => 'thought_contact_select' %>
我只是在控制器上查找与该朋友和该用户相关联的友谊
friendship = Friendship.find_by_user_id_and_friend_id(current_user.id, params[:friend_id])
has\u many
association访问友谊时,请使用has\u many:friendships,:order=>“name DESC”
参见此处的has\u many
说明::提示选项选择标签
,请参阅:
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end
class Friend < ActiveRecord::Base
has_many :friendships
has_many :users, :through => :friendships
end
第二个问题解决了!。但是,第一个问题不起作用,因为我正在调用current_user.friends,并且name位于第三个名为friends的表中,因此它给出了错误
SQLite3::SQLException:没有这样的列:name:选择“friendships”。*从“friendships”中的“friendships”。“user_id”=6按名称排序DESC
friendship = Friendship.find_by_user_id_and_friend_id(current_user.id, params[:friend_id])
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end
class Friend < ActiveRecord::Base
has_many :friendships
has_many :users, :through => :friendships
end
class User < ActiveRecord::Base
has_many :friendships
has_many :friends, :through => :friendships, :order => 'name'
end