我正在编写一个代码来处理读/未读消息,使用一个简单的user_id/message_id mysql表来处理读/未读状态.
当用户查看消息时,我执行
Reading.create(:user_id => uid, :message_id => mid)
Run Code Online (Sandbox Code Playgroud)
user_id/message_id字段组合中有唯一索引,因此当读数中的条目已经存在时,我得到有关重复条目的ActiveRecord :: StatementInvalid错误.
现在我可以补充一下
unless Reading.exists?(:user_id => uid, :message_id => mid)
Reading.create(:user_id => uid, :message_id => mid)
end
Run Code Online (Sandbox Code Playgroud)
但我想这会在INSERT之前再添加一个SELECT查询
我宁愿只有一个INSERT,即使它失败也没有错误报告(我猜REPLACE会是最好的,但是afaik它在ActiveRecord中不可用).
我有一组值(键),1,2,3,4,5和下表:
id key
------
1 2
2 8
3 4
Run Code Online (Sandbox Code Playgroud)
如果我需要检查哪个给定的键在db中,我使用:
SELECT *
FROM mytable
WHERE key IN (1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)
所以我会得到带有键2和4的行.
是否有某种mysql方法可以找出哪些给定键不在db中?所以在这种情况下我想得到1,3,5.
我注意到我的控制器测试开始变得太大,所以我将一些存根方法移动到一个单独的模块中.
我把它放在test/lib/my_module.rb中
module MyModule
def mymethod
end
end
Run Code Online (Sandbox Code Playgroud)
所以我在test/controllers/my_controller.rb中的控制器测试现在看起来像这样:
class MyControllerTest < ActionController::TestCase
include MyModule
def test_something
end
end
Run Code Online (Sandbox Code Playgroud)
然后我尝试在测试期间制作rails autoload path"test/lib".为此,我在config/environments/test.rb中添加了以下行
config.autoload_paths += ["#{config.root}/test/lib"]
config.eager_load_paths += ["#{config.root}/test/lib"]
Run Code Online (Sandbox Code Playgroud)
但是当我使用"RAILS_ENV = test bundle exec rake test"运行我的测试时,它会抛出:
rake aborted!
NameError: uninitialized constant MyControllerTest::MyModule
Run Code Online (Sandbox Code Playgroud)
如果我在config/application.rb中放入相同的两行,一切都运行良好.但我不想加载这个模块f.ex. 在生产中.
那为什么会发生,我该如何解决呢?什么是Rails保持测试专用代码的最佳实践?
我在我的网站上登录OmniAuthable Facebook.用户登录时使用:
link_to user_omniauth_authorize_path(:facebook)
Run Code Online (Sandbox Code Playgroud)
它在developers.facebook.com的app config中定义.
如果我通过www.example.com访问该网站,一切正常,我可以登录.
如果我在没有"www"子域(即example.com)的情况下访问它,我会得到以下响应:
{
"error": {
"message": "Invalid redirect_uri: Given URL is not allowed by the Application configuration.",
"type": "OAuthException"
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么解决这个问题?
我想在我的rails项目中使用soap4r和jira4r(ruby版本1.9.3)
在Gemfile我有:
gem 'soap4r', :git => 'git://github.com/felipec/soap4r.git'
gem 'jira4r-jh'
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的模型中运行时
require "jira4r/jira-tool"
model Mymodel
def mydef
jira = Jira4R::JiraTool.new(2, "http://my.website.com/")
jira.login("test@test.com","password")
end
end
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
uninitialized constant REXML::Element
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:129:in `_obj2soap'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:54:in `block (2 levels) in objs2soap'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:52:in `upto'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:52:in `block in objs2soap'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:560:in `block in protect_mapping'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:533:in `protect_threadvars'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:549:in `protect_mapping'
rubyjedi-soap4r (1.5.8.01) lib/soap/mapping/mapping.rb:51:in `objs2soap'
rubyjedi-soap4r (1.5.8.01) lib/soap/rpc/proxy.rb:475:in `request_rpc_enc'
rubyjedi-soap4r (1.5.8.01) lib/soap/rpc/proxy.rb:457:in `request_rpc'
rubyjedi-soap4r (1.5.8.01) lib/soap/rpc/proxy.rb:412:in `request_body'
rubyjedi-soap4r …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
def mymethod(a)
a.replace("a")
end
mystring = "b"
mymethod(mystring)
p mystring # => "a"
Run Code Online (Sandbox Code Playgroud)
但我想用Integer执行相同的操作
那可能吗?
我在一个页面上有两个相同的collection_selects(一个消息属于2组)
<%=
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} )
%>
<%=
collection_select(:message,:group_ids, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} )
%>
Run Code Online (Sandbox Code Playgroud)
是否可以使用collection_select为它们设置两个不同的选定值?
编辑:
我想我必须做点什么
<%
@message.group_id=5
%>
<%=
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} )
%>
<%
@message.group_id=6
%>
<%=
collection_select(:message,:group_id, Group.find(:all),:id, :title, {}, {:name=>'message[group_ids][]'} )
%>
Run Code Online (Sandbox Code Playgroud)
但当然它不起作用,并给出方法遗漏错误
编辑2:
猜猜没有办法用collection_select来做.除非group有方法,每次返回单个group_id.
我最终得到的是
select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group1.id)
select_tag 'message[group_ids][]', "<option></option>"+options_from_collection_for_select(Group.find(:all), 'id', 'title',group2.id)
Run Code Online (Sandbox Code Playgroud) 例如
str_modelname="User"
Run Code Online (Sandbox Code Playgroud)
而且我想这样做
str_modelname.find(:first)
Run Code Online (Sandbox Code Playgroud)
找到第一个用户,但当然这种方式不起作用
我正在尝试使用版本历史开发一个wiki.
我的计划是:每次编辑维基内容时,都应该保存为新内容.
现在,我有两个模型,Wiki和WikiContent,并在其中包含以下代码:
class Wiki < ActiveRecord::Base
has_many :wiki_contents
has_one :current_wiki, :class_name => "WikiContent"
accepts_nested_attributes_for :current_wiki
has_one :new_content, :class_name => "WikiContent"
accepts_nested_attributes_for :new_content
end
class WikiContent < ActiveRecord::Base
belongs_to :wiki
end
Run Code Online (Sandbox Code Playgroud)
Wiki模型有一个字段current_id,以了解哪个内容是当前内容.
在Wiki控制器中运行
def new
@wiki.build_current_wiki
end
def create
@wiki=Wiki.new(params[:wiki])
@wiki.save
@wiki.current_id=@wiki.current_wiki.id
end
Run Code Online (Sandbox Code Playgroud)
但每当我尝试运行:
def edit
@wiki.build_new_content
end
Run Code Online (Sandbox Code Playgroud)
它为current_wiki.wiki_id分配NULL.
我怎么能解决这个问题?还是有另一种方法让这个工作?
牧羊人的has_many动物.我试图克隆其中一个:
dolly=shepherd.animals.build(sheep.clone)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
undefined method `stringify_keys!' for #<Sheep:0xb6ce154c>
Run Code Online (Sandbox Code Playgroud)
为什么?什么是克隆小车的另一种方式,以便她与牧羊人联系并拥有绵羊的属性?
当我跑
rake test --trace
Run Code Online (Sandbox Code Playgroud)
这是发生了什么
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units
/usr/bin/ruby1.8 -I"lib:test".... (and after that fails because there's no fixtures loaded)
Run Code Online (Sandbox Code Playgroud)
为什么不加载灯具(我认为这将是默认行为)以及如何在执行测试之前使其加载灯具?
PS …
我有一个模型组和模型用户
它们通过"has_many:through => groups_users"连接到两个方向
groups_users表有一个名为moderator的属性,指定用户是否是该组的主持人
当我试图更新加入模块属性时,我得到一个错误
我现在这样做的方式:
@group_membership=@group.groups_users.find_by_user_id(@user.id)
if @group_membership!=nil
@group_membership.moderator=true
else
@group_membership=GroupsUser.new(:user_id => @user.id, :group_id => @group.id, :moderator => true)
end
@group_membership.save
Run Code Online (Sandbox Code Playgroud)
代码正在产生mysql错误:
Unknown column 'id' in 'where clause': UPDATE `groups_users` SET `moderator` = 1 WHERE `id` = NULL
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,或者我应该将id列添加到groups_users连接模型表并将其索引到id?
假设我有一个多维哈希,并且在其中一个子哈希中我有一个key => value对,我需要通过key检索它.我该怎么做?
示例哈希:
h={:x=>1,:y=>2,:z=>{:a=>{:k=>"needle"}}}
h={:k=>"needle"}
Run Code Online (Sandbox Code Playgroud)
关键是永远:k,我需要得到"针"
我注意到红宝石1.8中的哈希没有"扁平"功能,但如果它在那里,我想我会做的
h.flatten[:k]
Run Code Online (Sandbox Code Playgroud)
我想我需要为此编写一个递归函数?
谢谢