2012-10-18 2 views
0

매우 특별한 방식으로 데이터베이스를 시드하려고합니다. 다음은 /db/seeds.rb 파일입니다.rake db : seed가 예상대로 작동하지 않는 이유는 무엇입니까?

# creates a school in a local database. 
test_school = School.find_or_create_by_email(school_name: "Test School One", client_user_name: 'admin', client_password: 'Secretz1', fname: 'John', lname: 'doe', client_short_code: "72727", email: '[email protected]') 

# creates a user in local database 
test_user = User.find_or_create_by_email(email: test_school.email, password: test_school.client_password, full_name: test_school.full_name, user_role: 'admin', school_id: test_school.id) 

# creaate school via API 
results = School.create_via_api test_school 

if results[0].to_i == 0 
    test_school.update_attribute :client_number, results[2].split(" ").first 
    SchoolMailer.registration_confirmation(test_user, test_school).deliver 
else 
    test_school.destroy 
    test_user.destroy 
end 

# an array of keyword topics 
keywords_ary = ["lunch", "schoolout?", "thenews", "holidays", "buses", "help"] 
# create a keyword object for each topic in the above array 
keywords_ary.each do |n| 
    message = "" 
    Array(1..5).each do |i| 
    message += "#{i}. Test for @ts#{n}\n" 
    end 
    Keyword.find_or_create_by_user_id(
    name: "@ts#{n} test", keyword: "@ts#{n}", 
    message1: message, 
    user_id: test_user.id 
    ) 
end 

# create each of the keywords via API 
test_user.keywords.each do |keyword| 
    results = Keyword.create_on_avid keyword, test_user 
    if results[0].to_i == 0 
    #save the api id to the local database 
    keyword.update_attribute :keyword_id, results[2] 
    else 
    keyword.destroy 
    end 
end 

그래서 무엇이 문제입니까? 글쎄, 난 내 데이터베이스를 확인하는 경우에만 첫 번째 키워드는 만들어지고 키워드에 대한 message1 필드는 다음과 같습니다 : 그것은 다음과 같이해야 할 때

--- - 1 - 2 - 3 - 4 - 5 

: 내가 뭐하는 거지

1. this is a test for @tslunch 
2. this is a test for @tslunch 
3. this is a test for @tslunch 
4. this is a test for @tslunch 
5. this is a test for @tslunch 

뭔가 잘못? 당신은 그래서 처음이 그 user_id를 가진 객체를 생성하고 그 때마다 후가 동일한 개체를 가져옵니다 find_or_create_by_user_id

을하고 사촌

+0

관심의 대상 : 왜 모든 것이 이중 간격입니까? –

+0

모르겠다. 제 편집자가 아닙니다. 복사하고 붙여 넣었지만 간격에 실제로주의하지 않았습니다. –

+1

확인. 나는 너를 위해 그것을 고쳤다. 이 message1 필드는 어디에 있습니까? 어떻게 그 값을 데이터베이스에서 검색하고 있습니까? –

답변

1
Keyword.find_or_create_by_user_id 

이, 범인처럼 보인다.

Keyword.find_or_create_by_message1 
+0

내 게시글 때 귀하의 답변을 알지 못했지만 감사합니다! –

0

에 내 방식의 오류를 발견 한 것을 변경 될 수 있습니다. 내 keywords_ary.each 루프에서 Keyword.find_or_create_by_user_id을 사용하여 각 키워드를 만들었습니다. 문제는 각 키워드가 동일한 user_id를가집니다. 따라서 첫 번째 키워드가 발견되거나 생성 된 후 루프가 종료됩니다.

Keyword.find_or_create_by_user_id_and_name으로 변경했는데 제대로 작동하지 않았습니다.

관련 문제