我有控制器:
class AccountController < ApplicationController
def index
end
private
def current_account
@current_account ||= current_user.account
end
end
Run Code Online (Sandbox Code Playgroud)
如何current_account使用rspec 测试私有方法?
PS我使用Rspec2和Ruby on Rails 3
鉴于以下内容
class User < ActiveRecord::Base
has_and_belongs_to_many :companies
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)
您如何为公司和用户定义工厂,包括双向关联?这是我的尝试
Factory.define :company do |f|
f.users{ |users| [users.association :company]}
end
Factory.define :user do |f|
f.companies{ |companies| [companies.association :user]}
end
Run Code Online (Sandbox Code Playgroud)
现在我试试
Factory :user
Run Code Online (Sandbox Code Playgroud)
也许不出所料,这会导致无限循环,因为工厂递归地使用彼此来定义自己.
更令人惊讶的是,我没有提到如何在任何地方做到这一点,是否有一种模式来定义必要的工厂或我做了一些从根本上错误的事情?
ruby-on-rails associations has-and-belongs-to-many factory-bot
伙计们,
想确保我理解正确.请忽略继承的情况(SentientBeing),尝试着重于has_many中的多态模型:通过关系.也就是说,考虑以下......
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'"
has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_type = 'Alien'"
end
class Person < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class Alien < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class WidgetGrouping < ActiveRecord::Base
belongs_to :widget
belongs_to :grouper, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
在一个完美的世界里,我想,给一个Widget和一个人,做一些像:
widget.people << …Run Code Online (Sandbox Code Playgroud) 如果多个人正在处理项目并且数据库位置不同(特别是套接字),那么处理Rails database.yml的最佳方法是什么.
我有以下代码:
package main
import "net"
import "fmt"
import "bufio"
func main() {
conn, _ := net.Dial("tcp", "irc.freenode.net:6667")
reader := bufio.NewReader(conn)
go func() {
str, err := reader.ReadString('\n')
if err != nil {
// handle it
fmt.Println(err)
}
fmt.Println(str)
}()
}
Run Code Online (Sandbox Code Playgroud)
如果我没有从goroutine中的缓冲区读取的代码,它会输出这样的消息,这是我期望发生的:
:zelazny.freenode.net NOTICE * :*** Looking up your hostname...
Run Code Online (Sandbox Code Playgroud)
然而,将它放在goroutine里面什么都不打印.
有人可以解释为什么会这样吗?
我只是学习编程并决定尝试Ruby.我确定这是一个愚蠢的问题,但是教练正在讨论setter和getter方法,我很困惑.这是一个例子:
class Human
def noise=(noise)
@noise = noise
end
def noise
@noise
end
end
Run Code Online (Sandbox Code Playgroud)
从这个,实例化类,我可以把它说出来:
man = Human.new
man.noise=("Howdie!")
puts man.noise
Run Code Online (Sandbox Code Playgroud)
这导致了 Howdie!
现在令我困惑的是,教师说没有getter方法(两种方法中的第二种),就没有办法与实例变量@noise进行交互.
但是当我删除getter方法时,我仍然可以访问@noise,参见示例:
class Human
def noise=(noise)
@noise = noise
end
end
man = Human.new
puts man.noise=("Howdie!")
Run Code Online (Sandbox Code Playgroud)
这与使用它的getter方法相同.
所以现在我很困惑.为什么需要吸气剂?没有它,教师的意思是无法访问实例变量?他有可能使用旧版本的Ruby吗?
在此先感谢您的帮助.
在学习Rails时,我创建了一个应用程序,其Domains控制器嵌套在Customers控制器下面.我正在使用Rails 2.3.4,这是一次学习经历.我设法得到以下路由设置:
customer_domains GET /customers/:customer_id/domains(.:format) {:controller=>"domains", :action=>"index"}
POST /customers/:customer_id/domains(.:format) {:controller=>"domains", :action=>"create"}
new_customer_domain GET /customers/:customer_id/domains/new(.:format) {:controller=>"domains", :action=>"new"}
edit_customer_domain GET /customers/:customer_id/domains/:id/edit(.:format) {:controller=>"domains", :action=>"edit"}
customer_domain GET /customers/:customer_id/domains/:id(.:format) {:controller=>"domains", :action=>"show"}
PUT /customers/:customer_id/domains/:id(.:format) {:controller=>"domains", :action=>"update"}
DELETE /customers/:customer_id/domains/:id(.:format) {:controller=>"domains", :action=>"destroy"}
customers GET /customers(.:format) {:controller=>"customers", :action=>"index"}
POST /customers(.:format) {:controller=>"customers", :action=>"create"}
new_customer GET /customers/new(.:format) {:controller=>"customers", :action=>"new"}
edit_customer GET /customers/:id/edit(.:format) {:controller=>"customers", :action=>"edit"}
customer GET /customers/:id(.:format) {:controller=>"customers", :action=>"show"}
PUT /customers/:id(.:format) {:controller=>"customers", :action=>"update"}
DELETE /customers/:id(.:format) {:controller=>"customers", :action=>"destroy"}
root / {:controller=>"customers", :action=>"index"}
Run Code Online (Sandbox Code Playgroud)
但是,由于路由错误,域控制器的所有测试都会失败.
例如,以下测试(由Rails的资源生成器生成)失败,DomainsControllerTest类中的所有其他测试也失败.
class DomainsControllerTest …Run Code Online (Sandbox Code Playgroud) 我想过使用观察者或回调.什么时候应该使用观察者?
你可以做以下事情:
# User-model
class User << AR
after_create :send_greeting!
def send_greeting!
UserNotifier.deliver_greeting_message(self)
end
end
#observer
class UserNotifier << AR
def greeting_message(user)
...
end
end
Run Code Online (Sandbox Code Playgroud)
或者您可以创建一个观察者并让它在用户创建时观察...
你推荐什么?
当我尝试使用Paperclip gem上传图像时出现此错误:
NoMethodError(<ActionDispatch :: Http :: UploadedFile:0x000000025387f0>的未定义方法`stringify_keys')
class MenuItem < ActiveRecord::Base
has_one :image
end
class Image < ActiveRecord::Base
belongs_to :menu_item
has_attached_file :image, :styles => {
:large => "640x480",
:medium => "300x300",
:thumb => "100x100"
}
end
Run Code Online (Sandbox Code Playgroud) 我可以存储用户信用卡的到期日期和最后4位数吗?原因是我们可以通知用户他们的卡即将过期,并且他们应该将他们的帐户更改为新卡.存储最后四位数字将允许用户识别他们使用我们的系统存储的卡.
activerecord ×1
associations ×1
callback ×1
credit-card ×1
factory-bot ×1
file-upload ×1
go ×1
mysql ×1
nested ×1
paperclip ×1
routing ×1
rspec ×1
ruby ×1
security ×1
svn ×1
testing ×1