我在游戏和帐户模型之间有多对多的关系,如下所示:
class Account < ActiveRecord::Base
has_many :account_games, :dependent => :destroy
has_many :games, :through => :account_games
end
class Game < ActiveRecord::Base
has_many :account_games, :dependent => :destroy
has_many :accounts, :through => :account_games
end
class AccountGame < ActiveRecord::Base
belongs_to :account
belongs_to :game
end
Run Code Online (Sandbox Code Playgroud)
现在我知道让我们说我要创建一个类似的记录:
@account = Account.new(params[:user])
@account.games << Game.first
@account.save
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我应该如何更新AccountGame中的一些属性?假设AccountGame有一些字段叫做score,我该如何更新这个属性?你能告诉我最好的方法吗?在我保存对象时,在直通表中添加任何字段.
有什么方法可以知道工人是否已经完成了 Resque 中的特定工作/流程。场景:我有 5 个工人在做一些特定的过程,我想知道是否完成了过程以继续处理其他部分的代码。
如果有任何帮助,我正在使用 Ruby 1.8.7 和 Rails 3.1.1。
我有以下型号:user,role,user_role(user_role是一个连接模型)
我正在尝试使用用户#edit页面上的复选框编辑用户的角色.这是我的尝试,我觉得我错过了一些重要的事情,或采取了错误的方法.
has_many :user_roles, dependent: :destroy
has_many :roles, through: :user_roles
attr_accessible :user_roles_attributes
accepts_nested_attributes_for :user_roles, reject_if: lambda { |a| a[:role_id] == 0 }, allow_destroy: true
def has_role?(role_sym)
roles.any? { |r| r.name.underscore.to_sym == role_sym.downcase }
end
def setup_roles!
Role.all.each { |role|
user_roles.build(user_id: id, role_id: role.id) unless has_role?(role.name.to_sym)
}
end
Run Code Online (Sandbox Code Playgroud)
belongs_to :user
belongs_to :role
delegate :name, to: :role
Run Code Online (Sandbox Code Playgroud)
has_many :user_roles
has_many :users, through: :user_role
Run Code Online (Sandbox Code Playgroud)
def edit
@user = User.find(params[:id])
@user.setup_roles!
end …Run Code Online (Sandbox Code Playgroud) 我的团队在/net/CM/repo.git和的 NAS 服务器上有几个 git 存储库/net/CM/repo2.git。我最近加入了团队并向他们介绍了 GitLab,他们认为这将是一个很好的实施工具。
但是,他们希望我将 GitLab 安装在不同的服务器上,我们将其称为 Super Server,另一个我们将称为 NAS Server。
我已经在超级服务器上安装了 GitLab 并且它运行良好,但是我很难让它在 NAS 服务器上检测我的 git 存储库。那可能吗?如果可能的话,我不希望团队的其他成员不得不在他们的 git 存储库上更改他们的远程来源。我希望他们继续推送到标准存储库,GitLab 应该检测到更改。
我尝试git_data_dir按照Stack Overflow 帖子中提到的方式进行设置:
git_data_dir "/net/CM/"
Run Code Online (Sandbox Code Playgroud)
但是当我运行时sudo gitlab-ctl reconfigure,我最终会出现权限错误。它说它不能在/net/CM/.
我也尝试创建一个/net/git-data/目录,并将权限设置为 777 只是为了看看它是否有效,但无济于事。
总而言之,如何让 GitLab 的网络软件在我的超级服务器上运行,而我的所有存储库都在我的 NAS 服务器上?
更新
有人问我如何挂载NAS服务器。当我加入团队时,我遵循了提供给我的说明,但它们是:
# I pulled out the ip's and ports for security purposes.
sudo apt-get install nfs-common
sudo mkdir /net
sudo mount -t nfs -o proto=tcp,port=[port] …Run Code Online (Sandbox Code Playgroud) 我有一个表格可以将一个Parent和多个Child对象保存在一起.在初始化Child对象期间,它需要访问Grandparent.这是模型的样子:
class Grandparent
has_many :parents, inverse_of: :grandparent
end
class Parent
belongs_to :grandparent, inverse_of: :parents
has_many :children, inverse_of: :parent
accepts_nested_attributes_for :children
end
class Child
belongs_to :parent
delegate :grandparent, to: :parent
# Test code
after_initialize do
raise 'NoParentError' unless parent.present?
raise 'NoGrandparentError' unless grandparent.present? # Errors here!
puts 'All good!'
end
end
Run Code Online (Sandbox Code Playgroud)
请记住,表单用于同时保存新父项和多个子项,但我正在尝试访问祖父项对象中的信息.我读到inverse_of选项应该已经完成了这个伎俩,但child.grandparent仍然nil不幸.
这是实际导致失败的控制器部分:
@parent = @grandparent.parents.build(parent_params)
# prior to saving @parent
Run Code Online (Sandbox Code Playgroud)
由于某种原因,父母不知道祖父母是谁.
更新
看起来我可以通过以下代码解决该错误:
@parent = Parent.new(parent_params.merge(grandparent: …Run Code Online (Sandbox Code Playgroud) 所以我在这里有一个有点混乱的关系,在Note,Group和User之间.我最终在模型中使用了has_many两次.但我目前专注于Note&Group关系.
背景:一个小组可以有一个注释.用户也可以有笔记.这就是我的Note是多态的原因.但是,我还创建了一个名为Tag的连接模型,因此Note可以属于多个组.在我的代码中,我最终得到了多个'has_many:notes'.请参阅下面的所有代码.做这样的事情的正确方法是什么?
提前致谢!
belongs_to :notable, :polymorphic => true
has_many :tags
has_many :groups, :through => :tags
Run Code Online (Sandbox Code Playgroud)
has_many :notes, :as => :notable
Run Code Online (Sandbox Code Playgroud)
has_many :notes, :as => :notable
has_many :tags
has_many :notes, :through => :tags
Run Code Online (Sandbox Code Playgroud)
belongs_to :note
belongs_to :group
Run Code Online (Sandbox Code Playgroud) 在使用Rails时,我是否应该对所有html标记使用content_tag帮助程序?
Rails方式使用content_tag来处理Header标签之类的简单事情吗?
<%= content_tag :h2, :class => "bla bla" do %>
Header
<% end %>
Run Code Online (Sandbox Code Playgroud)
与
<h2>Header</h2>
Run Code Online (Sandbox Code Playgroud)
显然,使用直接html更"简单"和"更短",但正确的Rails做事方式是什么?
在可能有许多用户的 Rails 应用程序中使用“原始”助手是否安全?
我将把 TinyMCE 与我的应用程序集成,以便用户将 HTML 内容添加到某种形式的帖子中。使用“原始”显示其内容是否存在安全问题?
或者有更合适的做事方式吗?
谢谢!
将以下两个数组合并为多维数组的最佳方法是什么?
x = ['A', 'B', 'C']
y = ['D', 'E', 'F']
Run Code Online (Sandbox Code Playgroud)
期望的结果:
z = [['A', 'D'], ['A', 'E'], ['A', 'F'], ['B', 'D'], ['B', 'E'], ['B', 'F'], ['C', 'D'], ['C', 'E'], ['C', 'F']]
Run Code Online (Sandbox Code Playgroud) 使用byebuggem让我能够continue直到下一个断点:
(byebug) help
break -- Sets breakpoints in the source code
catch -- Handles exception catchpoints
condition -- Sets conditions on breakpoints
continue -- Runs until program ends, hits a breakpoint or reaches a line
delete -- Deletes breakpoints
disable -- Disables breakpoints or displays
display -- Evaluates expressions every time the debugger stops
down -- Moves to a lower frame in the stack trace
edit -- Edits source files
enable -- Enables breakpoints or displays …Run Code Online (Sandbox Code Playgroud) ruby ×4
associations ×2
activerecord ×1
background ×1
byebug ×1
git ×1
gitlab ×1
has-many ×1
helper ×1
html ×1
model ×1
nested-forms ×1
redis ×1
resque ×1
views ×1