编辑:这发生在开发中,但不是在生产中。这是一个问题的原因是因为当我school.tuition + school.room_and_board
为我的表添加时,它说'+'是一个无效的运算符和一个名为 nil 的对象。同样,这只发生在开发中,而不是生产中。尽管如此,这仍然是一个问题,因为我希望仍然能够在开发环境中工作。
请参阅http://www.collegeanswerz.com/colleges,然后查看大小。您会看到在国立大学下,创建了一个额外的 College 对象。我不知道为什么。
(注意:您看不到按字母顺序或排名,因为该代码是手动 html,而不是 rails 迭代)
我今天刚刚增加了 100 所学校。以前,没有创建额外的对象。现在有。
seeds.rb
@college = College.find_or_create_by_name!('University of Pittsburgh')
if @college.update_attributes(
url: 'university-of-pittsburgh',
public: 'Public',
years: '4-year',
category: 'National University',
calendar: 'Semester',
location: 'Pittsburgh, PA',
setting: 'Large City (250-500k)',
degrees: 'Certificate, Bachelors, Masters, Doctoral',
rotc: 'Air Force, Army, Navy',
very_important: 'Academic GPA|Rigor of secondary school record|Standardized Test Scores',
considered: 'Application Essay|Character/Personal Qualities|Class Rank|Extracurricular Activities|First Generation College Student|Geographical Residence|Level of Applicant\'s Interest|Racial/Ethnic Status|Recommendations|State Residency|Talent/Ability|Volunteer Work|Work Experience',
student_faculty: '14 to 1',
retention: '90',
graduation4: '57',
graduation6: '79',
sat_submit: '89',
act_submit: '41',
sat_math_25: '600',
sat_math_75: '690',
sat_reading_25: '570',
sat_reading_75: '690',
sat_writing_25: '560',
sat_writing_75: '660',
sat_composite_25: '1130',
sat_composite_75: '1320',
act_math_25: '25',
act_math_75: '30',
act_english_25: '25',
act_english_75: '31',
act_writing_25: '7',
act_writing_75: '9',
act_composite_25: '25',
act_composite_75: '30',
acceptance_rate: '58',
enrolled: '32',
gpa375: '71',
gpa35: '15',
gpa325: '8',
gpa3: '4',
gpa25: '2',
high_school_tenth: '54',
high_school_quarter: '86',
high_school_half: '99',
sat_math_700: '25',
sat_math_600: '53',
sat_math_500: '21',
sat_math_400: '1',
sat_math_300: '0',
sat_reading_700: '24',
sat_reading_600: '42',
sat_reading_500: '32',
sat_reading_400: '2',
sat_reading_300: '0',
sat_writing_700: '14',
sat_writing_600: '45',
sat_writing_500: '37',
sat_writing_400: '4',
sat_writing_300: '0',
act_composite_30: '33',
act_composite_24: '58',
act_composite_18: '8',
act_composite_12: '1',
act_math_30: '40',
act_math_24: '46',
act_math_18: '13',
act_math_12: '1',
act_english_30: '0',
act_english_24: '0',
act_english_18: '0',
act_english_12: '0',
class_20: '42',
class_49: '39.6',
class_50: '18.4',
majors: 'business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%',
law: '3',
business: '1',
medical: '4',
other_grad: '28',
us_news_ranking: '58',
top_25_grad: 'Physical Therapy (3)|Speech-Language Pathology (8)|Library and Information Studies (10)|Social Work (11)|Medicine (15)|Education (24)',
requirements: 'http://www.asundergrad.pitt.edu/requirements/gened.html',
majors_offered_link: 'http://www.pitt.edu/academics/undergraduate',
ap_credit: 'http://www.oafa.pitt.edu/pdf/CreditAPIBExam.pdf',
enrollment: '18427',
graduate_enrollment: '10339',
in_state: '68',
out_of_state: '32',
male: '49',
female: '51',
ethnicity: 'white: 77%|hispanic/latino: 2%|asian: 8%|black: 5%|2 or more races: 3%|non-resident aliens: 3%|other: 2%',
frats: '12',
sororities: '10',
on_campus: '45',
freshman_on_campus: '97',
in_state_tuition: '16590',
out_of_state_tuition: '26280',
room_and_board: '9870',
with_need_got_aid: '68',
got_need_fully_met: '7',
percent_need_met: '59',
scholarships: '41',
loans: '59',
cost_30: '17326',
cost_48: '19950',
cost_75: '23636',
cost_110: '26410',
cost_111: '26985',
scholarships_link: 'http://www.oafa.pitt.edu/universityschlrs.aspx',
map: '<iframe width="900" height="1150" scrolling="no" src="http://www.tour.pitt.edu/campus-map"></iframe>',
street_view: '<iframe width="900" height="550" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&aq=0&oq=uni&sll=40.444261,-79.970448&sspn=0.142165,0.251656&ie=UTF8&hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&ll=40.443486,-79.958245&spn=0.010798,0.021038&t=m&layer=c&cbll=40.441775,-79.956671&panoid=BgWQKMiSI1fpwP7JUF16Vg&cbp=12,45.25,,0,-3.34&output=svembed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&aq=0&oq=uni&sll=40.431368,-79.9805&sspn=0.142165,0.251656&ie=UTF8&hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&ll=40.443486,-79.958245&spn=0.010798,0.021038&t=m&layer=c&cbll=40.441775,-79.956671&panoid=BgWQKMiSI1fpwP7JUF16Vg&cbp=12,45.25,,0,-3.34" style="color:#0000FF;text-align:left">View Larger Map</a></small>',
housing: '<iframe src="http://www.tour.pitt.edu/tour-categories/residence-halls" scrolling="no" height="820" width="900"></iframe>',
food: '<div style="overflow: hidden; max-width: 1000px;"><iframe scrolling="no" src="http://www.pc.pitt.edu/dining/menus.php" style="margin-left: 0; height: 2000px; margin-top: -370px; margin-bottom: -375px; width: 750px;"></iframe></div>',
weather: '<div style="overflow: hidden; max-width: 500px;"><iframe scrolling="no" src="http://www.weather.com/weather/wxclimatology/monthly/graph/USPA1290" style="margin-left: -40px; height: 1570px; margin-top: -490px; width: 500px;"></iframe></div>',
dorms: 'Towers (fr.)|Sutherland (fr.)|Lothrop (fr.)|Nordenberg (fr.)|Holland (fr.)|Forbes (fr.; honors)|McCormick|Brackenridge|Amos|Bruce|Panther|Pennsylvania|Ruskin|Bouquet Gardens|Oakwood Apartments|Centre-Plaza Apartments|Forbes-Craig Apartments (honors)',
dorm_urls: 'towers|sutherland|lothrop|nordenberg|holland|forbes|mccormick|brackenridge|amos|bruce|panther|pennsylvania|ruskin|bouquet-gardens|oakwood-apartments|centre-plaza-apartments|forbes-craig-apartments',
science_majors: 'Actuarial Science|Agriculture|Applied Math|Astronomy|Biology|Chemistry|Computer Science|Earth Science|Geology|Linguistics|Mathematics|Neuroscience|Physics|Statistics',
social_science_majors: 'Anthropology|Criminology|Economics|History|Political Science|Psychology|Sociology|Urban Studies|Women\'s Studies',
humanities_majors: 'Art|Classics|English|Film|History and Philosophy of Science|Literature|Music|Philosophy|Religious Studies|Theatre',
business_majors: 'Accounting|Finance|Management|Marketing',
engineering_majors: 'Bioengineering|Chemical|Civil|Computer|Electrical|Engineering Science|Industrial|Materials Science and Engineering|Mechanical|Nuclear',
professional_majors: 'Architecture|Athletic Training|Communications/ Journalism|Information Science|Nursing|Nutrition|Pharmacy|Public Health|Rehab Science|Social Work',
area_studies_majors: 'African|Chinese|French|German|Italian|Japanese|Jewish|Latin American|Middle Eastern|Russian|Spanish|Other')
else
# handle validation errors
end
我的种子文件基本上是 300 所学校的种子文件。我看了很多次,没有看到任何应该创建一个空对象的东西。我使用 cmd + f 来查找 的变体@college = College.find_or_create_by_name!('')
,但什么也没有。我手动浏览了所有 cmd + f 的 for @college = College.find_or_create_by_name!(
,它们都在创建一个带有名称的实际大学(空对象没有名称)。
以下是一些相关的代码摘录。如果您想看更多,请告诉我。
关于问题是什么的任何想法?
static_pages_controller.rb
def colleges
@national_university = College.where("category = 'National University'").order("enrollment DESC")
@liberal_arts_college = College.where("category = 'Liberal Arts College' OR category = 'Military College'").order("enrollment DESC")
end
colleges.html.erb
<div class="tab-pane" id="size">
<h3>National Universities</h3>
<nav class="list">
<ol>
<% @national_university.each do |school| %>
<li><%= link_to "#{school.name} (#{(number_with_delimiter(school.enrollment, :delimiter => ',') )})", "/#{school.url}" %></li>
<% end %>
</ol>
</nav>
<h3>Liberal Arts Colleges</h3>
<nav class="list">
<ol>
<% @liberal_arts_college.each do |school| %>
<li><%= link_to "#{school.name} (#{(number_with_delimiter(school.enrollment, :delimiter => ',') )})", "/#{school.url}" %></li>
<% end %>
</ol>
</nav>
</div>
<div class="tab-pane" id="table">
<div class="tabbable">
<ul class="nav nav-pills">
<li class="active"><a href="#national_universities" data-toggle="tab">National Universities</a></li>
<li><a href="#liberal_arts_colleges" data-toggle="tab">Liberal Arts Colleges</a></li>
</ul>
</div>
<div class="tab-content">
<div id="national_universities" class="tab-pane active">
<table id="national_universities_table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Location</th>
<th>Setting</th>
<th>Size</th>
<th>Cost*</th>
<th>Math SAT</th>
<th>Reading SAT</th>
<th>ACT Composite</th>
</tr>
</thead>
<tbody>
<% @national_university.each do |school| %>
<tr>
<td class="name_column"><%= link_to "#{school.name}", "#{school.url}" %></td>
<td class="rank_column"><%= "#{school.us_news_ranking}" %></td>
<td class="location_column"><%= "#{school.location}" %></td>
<td class="setting_column"><%= "#{school.setting}" %></td>
<td class="size_column"><%= number_with_delimiter("#{school.enrollment}", :delimiter => ',') %></td>
<td class="cost_column">$<%= number_with_delimiter("#{school.in_state_tuition}", :delimiter => ',') %>
/ $<%= number_with_delimiter("#{school.out_of_state_tuition}", :delimiter => ',') %></td>
<td class="math_column"><%= "#{school.sat_math_25} - #{school.sat_math_75}" %></td>
<td><%= "#{school.sat_reading_25} - #{school.sat_reading_75}" %></td>
<td><%= "#{school.act_composite_25} - #{school.act_composite_75}" %></td>
</tr>
<% end %>
</tbody>
</table>
</div> <!-- pane 1 -->
<div id="liberal_arts_colleges" class="tab-pane">
<table id="liberal_arts_colleges_table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Location</th>
<th>Setting</th>
<th>Size</th>
<th>Cost*</th>
<th>Math SAT</th>
<th>Reading SAT</th>
<th>ACT Composite</th>
</tr>
</thead>
<tbody>
<% @liberal_arts_college.each do |school| %>
<tr>
<td class="name_column"><%= link_to "#{school.name}", "#{school.url}" %></td>
<td class="rank_column"><%= "#{school.us_news_ranking}" %></td>
<td class="location_column"><%= "#{school.location}" %></td>
<td class="setting_column"><%= "#{school.setting}" %></td>
<td class="size_column"><%= number_with_delimiter("#{school.enrollment}", :delimiter => ',') %></td>
<td class="cost_column">$<%= number_with_delimiter("#{school.in_state_tuition + school.room_and_board}", :delimiter => ',') %>
/ $<%= number_with_delimiter("#{school.out_of_state_tuition + school.room_and_board}", :delimiter => ',') %></td>
<td class="math_column"><%= "#{school.sat_math_25} - #{school.sat_math_75}" %></td>
<td><%= "#{school.sat_reading_25} - #{school.sat_reading_75}" %></td>
<td><%= "#{school.act_composite_25} - #{school.act_composite_75}" %></td>
</tr>
<% end %>
</tbody>
</table>
</div> <!-- pane 2-->
<p>Tip: Sort multiple columns simultaneously by holding down the shift key and clicking a second, third or even fourth column header!</p>
<p>*cost = tuition + room & board; in state / out of state</p>
</div> <!-- tab-content -->
</div> <!-- tab-pane -->
尝试删除空对象期间的控制台输出
:002 > College.where("name=''")
College Load (5.6ms) SELECT "colleges".* FROM "colleges" WHERE (name='')
=> [#<College id: 201, public: "Public", years: "4-year", category: "National University", calendar: "", location: "", setting: "", retention: nil, created_at: "2013-08-02 20:26:53", updated_at: "2013-08-02 20:26:53", graduation4: nil, graduation6: nil, degrees: "", rotc: "", sat_submit: nil, act_submit: nil, sat_math_25: nil, sat_math_75: nil, sat_reading_25: nil, sat_reading_75: nil, sat_writing_25: nil, sat_writing_75: nil, sat_composite_25: nil, sat_composite_75: nil, act_math_25: nil, act_math_75: nil, act_writing_75: nil, act_composite_25: nil, act_composite_75: nil, acceptance_rate: nil, enrolled: nil, gpa375: nil, gpa35: nil, gpa325: nil, gpa3: nil, gpa25: nil, high_school_tenth: nil, high_school_quarter: nil, high_school_half: nil, sat_math_700: nil, sat_math_600: nil, sat_math_500: nil, sat_math_400: nil, sat_math_300: nil, sat_reading_700: nil, sat_reading_600: nil, sat_reading_500: nil, sat_reading_400: nil, sat_reading_300: nil, sat_writing_700: nil, sat_writing_600: nil, sat_writing_500: nil, sat_writing_400: nil, sat_writing_300: nil, act_composite_30: nil, act_composite_24: nil, act_composite_18: nil, act_composite_12: nil, act_math_30: nil, act_math_24: nil, act_math_18: nil, act_math_12: nil, very_important: "", important: nil, considered: "", student_faculty: "", class_20: nil, class_49: nil, class_50: nil, majors: "", law: nil, business: nil, medical: nil, other_grad: nil, us_news_ranking: nil, top_25_grad: "", enrollment: nil, graduate_enrollment: nil, in_state: nil, out_of_state: nil, male: nil, female: nil, ethnicity: "", frats: nil, sororities: nil, on_campus: nil, freshman_on_campus: nil, in_state_tuition: nil, out_of_state_tuition: nil, room_and_board: nil, with_need_got_aid: nil, got_need_fully_met: nil, percent_need_met: nil, scholarships: nil, loans: nil, name: "", url: "", act_writing_25: nil, cost_30: nil, cost_48: nil, cost_75: nil, cost_110: nil, cost_111: nil, requirements: "", majors_offered: nil, ap_credit: "", scholarships_link: "", map: "", street_view: "", housing: "", food: "", weather: "", dorms: "", majors_offered_link: "", science_majors: "Actuarial Science|Agriculture|Applied Math|Astronom...", social_science_majors: "Anthropology|Criminology|Economics|History|Politica...", humanities_majors: "Art|Classics|English|Film|History and Philosophy of...", business_majors: "Accounting|Finance|Management|Marketing", engineering_majors: "Bioengineering|Chemical|Civil|Computer|Electrical|E...", professional_majors: "Architecture|Athletic Training|Communications/ Jour...", dorm_urls: "", act_english_25: nil, act_english_75: nil, act_english_30: nil, act_english_24: nil, act_english_18: nil, act_english_12: nil, area_studies_majors: "African|Chinese|French|German|Italian|Japanese|Jewi...">]
1.9.3p429 :003 > College.where("name=''").destroy
ArgumentError: wrong number of arguments (0 for 1)
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation.rb:378:in `destroy'
from (irb):3
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3p429 :004 > College.where("name=''").delete
ArgumentError: wrong number of arguments (0 for 1)
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/relation.rb:440:in `delete'
from (irb):4
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/adamzerner/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3p429 :005 >