Tag: 拥有并且属于许多

Formtastic / rails表格未提交

我正在尝试使用formtastic制作一个表格,我可以进入:反对选择a:场地和a:团队然后会出现一个我可以检查选择它们的球员列表 我已经设置了表单以便正确呈现,但是在提交时它不会保存任何信息并只是重新加载页面。 我的代码在我的github: https : //github.com/jpknegtel/st_francis 楷模 这涉及以下模型: 播放机 has_many :player_fixtures has_many :fixtures, :through => :player_fixtures 灯具 has_many :player_fixtures has_many :players, :through => :player_fixtures PlayerFixture belongs_to :player belongs_to :fixture 调节器 def create @fixture = Fixture.new(params[:fixture]) if @fixture.save flash[:notice] = “Fixture Created” redirect_to(:action =>’list’) else flash.now[:error] = “Could not save fixture. Please re-enter information” render(‘new’) end […]

模拟has_and_belongs_to_many嵌套在Rails 3中的行为

所以Rails不支持:通过关系进行关联。 有插件可以为Rails 2.x添加它,但我使用的是Rails 3 / Edge,只需要一个特定模型的关联。 所以我以为我会用Arel的美丽把自己弄得一团糟。 一,型号: class CardSet true end class Card true has_many :learnings, :dependent => :destroy end class Learning < ActiveRecord::Base belongs_to :card end 我希望获得属于特定卡片组的所有学习内容:through => cards。 这是我目前在CardSet模型中所拥有的: def learnings c = Table(Card.table_name) l = Table(Learning.table_name) j = Table(self.class.send(:join_table_name, Card.table_name, CardSet.table_name)) learning_sql = l.where(l[:card_id].eq(c[:id])).where(c[:id].eq(j[:card_id])).join(j).on(j[:card_set_id].eq(self.id)).to_sql Learning.find_by_sql(learning_sql) end 这给了我(该死的,Arel很漂亮!): SELECT `learnings`.`id`, `learnings`.`card_id`, `learnings`.`user_id`, […]

has_and_belongs_to_many或has_many用户/组关系?

我正在研究具有以下型号的Rails 3.1应用程序: 用户: class User ‘Group’ end 组: class Group ‘User’ end 它们之间有一个连接表,groups表也有一个“user_id”列。 我希望能够在我的groups_controller.rb中写这个 @group = Group.find(params[:id]) foo = @group.owner 但是当我这样做时,我会遇到以下错误: Mysql2::Error: Unknown column ‘users.group_id’ in ‘where clause’: SELECT `users`.* FROM `users` WHERE `users`.`group_id` = 1 LIMIT 1 我不明白为什么它甚至在寻找那个专栏。 任何帮助,将不胜感激!

rails 3 habtm只删除关联

class Company has_and_belongs_to_many :users end class User has_and_belongs_to_many :companies end 当我删除一家公司时,删除该公司用户的关联的最佳(推荐)方法是什么? (我的意思不是实际用户,只有关联)

Rails为has_and_belongs_to_many关系路由

我正在为一个运动队工作一个rails 4 API,我有球员和球队,而且我在使用rails routing和has_many关系方面有点挣扎。 我和球员之间的关系看起来像: class Team < ActiveRecord::Base extend Searchable validates :title, presence: true has_and_belongs_to_many :players end class Player < ActiveRecord::Base extend Searchable validates :first_name, presence: true validates :last_name, presence: true has_and_belongs_to_many :teams end 我希望能够将现有的玩家添加到团队中,但我不确定如何更改我的routes.rb文件。 目前,它看起来像: Rails.application.routes.draw do devise_for :users namespace :api, defaults: { format: :json }, constraints: { subdomain: ‘api’ }, path: ‘/’ […]