关于 ruby?? on rails:如何通过 has_and_belongs_to_many 关联获取 FactoryGirl 模型属性? | 珊瑚贝

How to get FactoryGirl model attributes with has_and_belongs_to_many association?


我正在尝试使用 HABTM 关联 :countries 获取 FactoryGirl.attributes_for(:trip),因为控制器测试失败 – :countries 在 :trip 属性中不存在):

旅行控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class TripsController < ApplicationController
  def create
    trip = Trip.new(create_params)
    if trip.save
      redirect_to trips_path, notice: ‘Trip successfully created.’
    else
      redirect_to :back, alert: trip.errors.full_messages.join().html_safe
    end
  end

  def create_params
    params.require(:trip).permit(:end_date, :description, country_ids: [], countries: [])
  end
end

RSpec TripsController 测试:

1
2
3
4
5
6
describe TripsController do
  describe ‘POST #create’ do
    before { post :create, trip: attributes_for(:trip) }
    it { is_expected.to redirect_to trips_path }
  end
end

旅行模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Trip < ActiveRecord::Base
  # Associations
  has_and_belongs_to_many :countries

  #Validations
  validate :require_at_least_one_country

  private

  def require_at_least_one_country
    if country_ids.empty? && countries.count == 0
      errors.add(:base, ‘Please select at least one country’)
    end
  end
end

旅行工厂:

1
2
3
4
5
6
7
8
9
10
FactoryGirl.define do
  factory :trip do
    description  { Faker::Lorem.sentence }
    end_date  { DateTime.now + 1.day }

    after(:build) do |trip, evaluator|
      trip.countries << FactoryGirl.create(:country, :with_currencies)
    end
  end
end

ruby文件:

1
factory_girl_rails (4.5.0)

试过这个:http://makandracards.com/jan0sch/11111-rails-factorygirl-and-has_and_belongs_to_many,但没用。

  • 我曾经尝试将 has_and_belongs_to_many 用于多对多,但曾经遇到各种问题。我发现使用 has_many 设置起来要容易得多。本教程可能会有所帮助:blog.teamtreehouse.com/…


下面是答案和解释:

1
2
3
4
5
6
7
8
9
10
FactoryGirl.define do
  factory :trip do
    description  { Faker::Lorem.sentence }
    end_date  { DateTime.now + 1.day }

    after(:build) do |trip, evaluator|
      trip.countries << FactoryGirl.create(:country, :with_currencies)
    end
  end
end

FactoryGirl.attributes_for(:trip) 返回

1
2
3
4
{
  :description=>“Eum alias tenetur odit voluptatibus inventore qui nobis.”,
  :end_date=>Wed, 16 Sep 2015 11:48:28 +0300
}

.

1
2
3
4
5
6
7
FactoryGirl.define do
  factory :trip do
    description  { Faker::Lorem.sentence }
    end_date  { DateTime.now + 1.day }
    country_ids { [FactoryGirl.create(:country, :with_currencies).id] }
  end
end

FactoryGirl.attributes_for(:trip) 返回

1
2
3
4
5
{
  :description=>“Assumenda sapiente pariatur facilis et architecto in.”,
  :end_date=>Wed, 16 Sep 2015 11:45:22 +0300,
  :country_ids=>[4]
}

检查您是否真的在请求中发送了任何 countries_ids。


请用 attributes_for(:trip) 的值更新您的帖子。

  • 嗨 Alexey,问题不在 Controller 中,而是在 FactoryGirl.attributes_for
  • 嗨,实际上我的意思是您没有在这行代码中发送 countries_ids:before { post :create, trip: attributes_for(:trip) },但我不很高兴你自己解决了。


来源:https://www.codenong.com/32558059/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8992.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?