class Person < ActiveRecord::Base
has_many :pets
scope :with_dog, join(:pets).where("pets.type = 'Dog'")
scope :without_pets ???????????????????????????????????
end
class Pet < ActiveRecord::Base
belongs_to :people
end
Run Code Online (Sandbox Code Playgroud)
我想在Person模型中添加一个范围,返回没有宠物的人.有任何想法吗?我觉得这很明显,但此刻它正在逃避我.
我有两个模型:用户和公司.它们都是从一个表单创建的,我正在使用这样的事务:
User.transaction do
@user.save!
@company.user = @user
@company.save!
@user.reload
@user.company = @company
@user.save!
flash[:notice] = "Thank you for your registration."
redirect_to_index
end
Run Code Online (Sandbox Code Playgroud)
即使公司的某个验证失败,用户也会保存到数据库中.我已经尝试添加ActiveRecord :: RecordInvalid的显式错误处理,但它没有帮助.我认为验证会引发错误以回滚事务.任何帮助是极大的赞赏.
谢谢
我发现了一种基于格式在过滤器之前跳过的方法,如下所示,但我想知道是否有更好的方法,因为这会使事情变得混乱并且不是很干.
before_filter do |controller|
:current_cart unless controller.request.format.js?
end
Run Code Online (Sandbox Code Playgroud)
如果我不这样做,json请求失败,因为current_cart方法和其他方法只做html的事情.
我正在努力找到使回形针网址安全的最佳方法,但仅限于安全页面.
例如,显示存储在S3中的图像的主页是http://mydomain.com,图像URL是http://s3.amazonaws.com/mydomainphotos/89/thisimage.JPG?1284314856.
我有像https://mydomain.com/users/my_stuff/49这样的安全页面,其中的图像存储在S3中,但S3协议是http而不是https,因此用户会从浏览器收到一条警告,说明某些元素在页面不安全,等等等等.
我知道我可以在模型中指定:s3_protocol,但这使得一切都安全,即使没有必要.所以,我正在寻找将协议更改为https的最佳方法,仅用于安全页面.
一种(可能是坏的)方法是创建一个新的url方法,如:
def custom_url(style = default_style, ssl = false)
ssl ? self.url(style).gsub('http', 'https') : self.url(style)
end
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是,我正在使用ssl_requirement插件,因此可能有一种方法可以将其与之相关联.
我确信有一些简单,标准的方法可以做到这一点,我忽略了,但我似乎无法找到它.
我正在构建一个电子应用程序,需要在API提供程序未启用CORS的情况下调用API。通常提出的解决方案是使用反向代理,当通过使用node和cors进行本地运行时,这样做很简单:
let port = (process.argv.length > 2) ? parseInt (process.argv[2]) : 8080;
require ('cors-anywhere').createServer ().listen (port, 'localhost');
Run Code Online (Sandbox Code Playgroud)
然后可以将该应用程序配置为通过localhost:8080上的反向代理来代理所有请求。
因此,我的问题是:
是否可以在电子应用程序中的任何位置使用节点和cors来创建反向代理?我不想强迫应用程序调用远程服务器。
在Electron应用程序中,有没有更好的方法或标准的方法?我假设我不是第一个遇到CORS问题的人。:)
这可能是一个愚蠢的问题,但我似乎无法找到一个好的答案.我想知道引用一个对象所属模型的最佳方法.
例如:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :users
end
Run Code Online (Sandbox Code Playgroud)
所以,为了获得用户的帖子,我可以使用user.posts,但是为了获得帖子的用户,我不能这样做:post.user
如果我向Post模型添加"用户"方法,它可以工作,但它似乎不是最好的方法.
class Post < ActiveRecord::Base
belongs_to :users
def user
User.find(self.user_id)
end
end
Run Code Online (Sandbox Code Playgroud)
如果你看这篇博客文章http://www.fortytwo.gr/blog/18/9-Essential-Rails-Tips作为例子,你可以看到作者使用post.user.username,这不起作用开箱即用以及:include => [:user],即使使用Post模型中的"user"方法也不起作用.
我知道这很简陋,所以感谢你的耐心等待.我只是想知道实现这种关系的最佳方法.
我的主要目标是使用嵌套包含来编写"查找",它会像这样引用给用户:
post = Post.find(:all,:include => [:user])
当我尝试这个时,我得到"ActiveRecord :: ConfigurationError:未找到名为'user'的关联;也许你拼错了它?"
非常感谢.
我有帐户和帐户地址.一个帐户可以有多个AccountAddressess,我想指定一个作为"default_account_address",因此在Account表中,我有一个名为"default_account_address_id"的列.这是模型的当前状态.
class Account < ActiveRecord::Base
has_many :account_addresses
belongs_to :default_account_address,
:class_name => "AccountAddress",
:inverse_of => :account_assigned_to_as_default
end
class AccountAddress < ActiveRecord::Base
belongs_to :accounts
has_one :account_assigned_to_as_default,
:class_name => "Account",
:foreign_key => :default_account_address_id,
:inverse_of => :default_account_address
end
Run Code Online (Sandbox Code Playgroud)
除了@ account.default_account_address返回帐户地址和@ account.account_addresses返回空数组这一事实外,这种方法很好.
因此,问题是默认帐户地址不包含在@ account.account_addresses中.
有关解决此问题的最佳方法的任何想法?我考虑过habtm,但这似乎不合适.我考虑使用has_one:default_account_address,但这没有意义,因为default_account_address_id列在帐户表上.谢谢.
此字符串替换有效:
"reverse, each word".gsub(/(\w+)/, "\\1a")
=> "reversea, eacha worda"
Run Code Online (Sandbox Code Playgroud)
和这样,单引号基本相同:
"reverse, each word".gsub(/(\w+)/, '\1a')
=> "reversea, eacha worda"
Run Code Online (Sandbox Code Playgroud)
但如果我试图反转字符串,它会失败:
"reverse, each word".gsub(/(\w+)/, "\\1a".reverse)
=> "a1\\, a1\\ a1\\"
Run Code Online (Sandbox Code Playgroud)
我玩过它,但似乎无法让相反的操作起作用.
belongs-to ×1
cors ×1
electron ×1
has-one ×1
https ×1
include ×1
json ×1
model ×1
node.js ×1
paperclip ×1
proxy ×1
regex ×1
relationship ×1
ruby ×1
scope ×1
secure-scl ×1
string ×1
transactions ×1