English 中文(简体)
如何做——通过:与固定装置的联系?
原标题:How to make has_many :through association with fixtures?

I can t use Factory_ girls 由于Im检测站需要真正的数据库。

http://www.ohchr.org。 它可以与太阳站合作。 我错了。

我怎样才能建设:通过(a.k.a 很多到男性)协会。

http://osdir.com/ml/RubyonRailsTalk/2010-06/msg00199.html

http://www.ohchr.org。

最后,我使用工厂——女孩。 I google-copy-paste a snippet:

factory :tagging do 
    question { |a| a.association(:question) } 
    tag { |a| a.association(:tag) } 
end

(问题有:通过tag子tag,反之亦然)

It works well. But what s it? The factory_girl s readme didn t meantion this syntax. Could someone explain?

最佳回答

你们可以找到工厂妇女的官方文件,该文件非常完整,here

rel=“nofollow”>Here是一家修饰厂,解释工厂的女生人数为2 (与工厂女生相比)。

执行:

a. 对法典作解释:

 factory :tagging do
   association :tag
 end

将寻找一个叫做的工厂:tag,并将构造该物体,然后与协会链接tag/code>(例如belongs_to)。 页: 1

Please note: this is the default factory. If you want taggings to share a tag, you will need to do something like

@tag = Factory(:tag)
@tagging_1 = Factory(:tagging, :tag => @tag)
@tagging_2 = Factory(:tagging, :tag => @tag)

希望这一帮助。

问题回答

如果在协会模式中没有其他信息,它就是一个传统组织——和——归属者——来信——的话,我认为这些公约允许你写像你这样的东西:

#users.yml
john:
  first_name: John
  last_name: Doe
  hobbies: [swim, play_tennis]

#hobbies.yml
swim:
  name: Swim

play_tennis:
  name: Play Tennis

但我并不完全相信!

我使用了<代码>has_many>的测试装置:通过,通过<代码>。

# posts.yml
one:
  title: "Railscasts"
  url: "http://railscasts.com/"
  description: "Ruby on Rails screencasts"

# categories.yml
one:
  name: "magazine"
two:
  name: "tutorial"
three:
  name: "news"
four:
  name: "Ruby"

# posts_controller_test.rb
def test_post_create
  assert_difference  Post.count  do
    post :create, post: posts(:one).attributes
     .merge(categories: [categories(:two), categories(:four)])
  end
end

在添加了另一处固定装置后,经过审判后,它就没有工作。

# post_categories.yml
one:
  post: one
  category: two
two:
  post: one
  category: four

def test_post_create
  assert_difference  Post.count  do
    post :create, post: posts(:one)
  end
end

puts posts(:one).attributes
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:27:20 UTC +00:00}

puts posts(:one).attributes
      .merge(categories: [categories(:two), categories(:four)])
# {"id"=>980190962, "url"=>"http://railscasts.com/", "title"=>"Railscasts", "description"=>"Ruby on Rails screencasts", "created_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "updated_at"=>Thu, 14 May 2015 18:30:23 UTC +00:00, "category_ids"=>[980190962, 1018350795]}




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签