我有三个型号:
class ReleaseItem < ActiveRecord::Base
has_many :pack_release_items
has_one :pack, :through => :pack_release_items
end
class Pack < ActiveRecord::Base
has_many :pack_release_items
has_many :release_items, :through=>:pack_release_items
end
class PackReleaseItem < ActiveRecord::Base
belongs_to :pack
belongs_to :release_item
end
Run Code Online (Sandbox Code Playgroud)
问题是,在执行期间,如果我将一个包添加到release_item,则它不知道该包是一个包.例如:
Loading development environment (Rails 2.1.0)
>> item = ReleaseItem.new(:filename=>'MAESTRO.TXT')
=> #<ReleaseItem id: nil, filename: "MAESTRO.TXT", created_by: nil, title: nil, sauce_author: nil, sauce_group: nil, sauce_comment: nil, filedate: nil, filesize: nil, created_at: nil, updated_at: nil, content: nil>
>> pack = Pack.new(:filename=>'legion01.zip', :year=>1998)
=> #<Pack id: nil, filename: "legion01.zip", created_by: …Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要处理用户时区的日期和时间的Rails应用程序.我们最近将它迁移到Rails 2.1并添加了时区支持,但是在很多情况下我们使用Time#utc然后与那个时间进行比较.这与原始Time对象的比较不一样吗?
何时在Rails 2.1中使用Time#utc是否合适?什么时候不合适?
想象一下用Ruby on Rails编写的Web应用程序.该应用程序的部分状态表示在一个不符合模型描述的数据中.此状态描述符需要与模型保持在同一数据库中.它与模型的不同之处在于,它只需要一个类的实例,并且它与其他类没有关系.
有没有人遇到这样的事情?
假设我正在为已经拥有People应用程序的出版公司编写一个Library应用程序.
所以在我的图书馆应用程序中我有
class Person < ActiveResource::Base
self.site = "http://api.people.mypublisher.com/"
end
Run Code Online (Sandbox Code Playgroud)
现在我想Article为每个存储s Person:
class Article < ActiveRecord::Base
belongs_to :person, :as => :author
end
Run Code Online (Sandbox Code Playgroud)
我想我的数据库中有以下表格:
Articles
id (PK) | title (string) | body (text) | author_id (integer)
Run Code Online (Sandbox Code Playgroud)
author_id不完全是外键,因为我没有People表.这留下了几个问题:
我怎么告诉我的Person ActiveResource对象呢has_many Articles?
会Articles.find(:first).author工作吗?会belongs_to甚至工作因为没有ActiveRecord,没有支撑台?
我正在写一个Rails插件,其中包含一些部分内容.我想测试部分内容,但是我很难设置一个可以渲染它们的测试.没有相关的控制器,所以我只假装一个:
require 'action_controller'
require 'active_support'
require 'action_pack'
require 'action_view'
class MyTest < Test::Unit::TestCase
def setup
@renderer = ActionController::Base.new
@renderer.append_view_path File.expand_path(File.join(File.dirname(__FILE__), '..', 'views'))
end
def test_renders_link
result = @renderer.render(:partial => '/something')
assert ...
end
end
Run Code Online (Sandbox Code Playgroud)
但那个:render电话总是爆炸.我尝试使用的是一个ActionView::Base而不是一个ActionController::Base,但这种情况甚至更少.
有人有成功吗?
在试图使我的Javascript不引人注目时,我正在使用onLoads来为<input>s等添加功能.使用Dojo,这看起来像:
var coolInput = dojo.byId('cool_input');
if(coolInput) {
dojo.addOnLoad(function() {
coolInput.onkeyup = function() { ... };
});
}
Run Code Online (Sandbox Code Playgroud)
或者,大致相当于:
dojo.addOnLoad(function() {
dojo.forEach(dojo.query('#cool_input'), function(elt) {
elt.onkeyup = function() { ... };
});
});
Run Code Online (Sandbox Code Playgroud)
有没有人写Ruby的的实现andand,这样我可以做到以下几点?
dojo.addOnLoad(function() {
// the input's onkeyup is set iff the input exists
dojo.byId('cool_input').andand().onkeyup = function() { ... };
});
Run Code Online (Sandbox Code Playgroud)
要么
dojo.byId('cool_input').andand(function(elt) {
// this function gets called with elt = the input iff it exists
dojo.addOnLoad(function() {
elt.onkeyup = function() { ... …Run Code Online (Sandbox Code Playgroud) 我的一个sui模型中有一个字段.它代表"标准用户标识符".当字段上存在验证错误时,Rails会打印出"Sui is required"或"Sui已被占用".
我怎么能告诉Rails 'sui'.titleize是"SUI"?我看着Inflector.human,但那不太对劲.
我有以下XML文档:
<samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217">
<saml:NameID>@NOT_USED@</saml:NameID>
<samlp:SessionIndex>abcdefg</samlp:SessionIndex>
</samlp:LogoutRequest>
Run Code Online (Sandbox Code Playgroud)
我想得到它的内容SessionIndex(即'abcdefg').我试过这个:
XPATH_QUERY = "LogoutRequest[@ID][@Version='2.0'][IssueInstant]/SessionIndex"
SAML_XMLNS = 'urn:oasis:names:tc:SAML:2.0:assertion'
SAMLP_XMLNS = 'urn:oasis:names:tc:SAML:2.0:protocol'
require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.xpath(XPATH_QUERY, 'saml' => SAML_XMLNS, 'samlp' => SAMLP_XMLNS)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Nokogiri::XML::SyntaxError: Namespace prefix samlp on LogoutRequest is not defined
Nokogiri::XML::SyntaxError: Namespace prefix saml on NameID is not defined
Nokogiri::XML::SyntaxError: Namespace prefix samlp on SessionIndex is not defined
Run Code Online (Sandbox Code Playgroud)
我已经尝试将命名空间添加到XPath查询中,但这并没有改变任何东西.
为什么我不能说服Nokogiri命名空间是有效的?
我有以下XML:
<root>
<foo>
<bar type="a whole bunch of stuff, then a magic string: MUPPET" />
<value>my Muppet value</value>
</foo>
<foo>
<bar type="some other stuff, then a different magic string: GREMLIN" />
<value>my Gremlin value</value>
</foo>
</root>
Run Code Online (Sandbox Code Playgroud)
我想构建一个XPath查询,在查看魔术字符串"MUPPET"时返回"my Muppet value"(字符串).我的猜测是:
/root/foo[contains(bar/@type,'MUPPET')]/value/text()
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我真的不确定该contains(x,y)运算符是否允许查询作为第一个参数.作为一个侧面问题,我不确定我是否需要text()最终.
有帮助吗?
我正在尝试在我的 YARD 文档中创建一些链接。我可以得到一个 HTTP 链接:
# I like {http://stackoverflow.com Stackoverflow}
Run Code Online (Sandbox Code Playgroud)
呈现为
<p>I like <a href="http://stackoverflow.com">Stackoverflow</a></p>
Run Code Online (Sandbox Code Playgroud)
但是一个电子邮件链接:
# You can email the {mailto:bugs@myproject.com bugs} list
Run Code Online (Sandbox Code Playgroud)
给我一个警告:
[warn]: In file `':: Cannot resolve link to mailto:bugs@myprojectmailto:bugs@myproject.com from text:
Run Code Online (Sandbox Code Playgroud)
并呈现为
<p>You can email the <tt>bugs</tt> list</p>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过链接的常规 RDoc 语法:
bugs[mailto:bugs@myproject.com]
Run Code Online (Sandbox Code Playgroud)
但 YARD 似乎忽略了这一点。有谁知道更可靠的语法?
ruby ×8
activerecord ×2
xml ×2
xpath ×2
actionview ×1
andand ×1
javascript ×1
nokogiri ×1
persistence ×1
plugins ×1
rdoc ×1
testing ×1
yard ×1