Rails / MySQL只将一个属性值保存在列中,保存到DB

时间:2012-11-20 19:08:20

标签: mysql ruby-on-rails activerecord

这将是一个难以提供输入的问题所以我会尽可能多地提供详细信息。以下是我想弄清楚的确切问题。由于某些原因,当我保存时,经验是删除列中的所有其他值并在更新日期写入。我只是保存了所有其他值,但为了清晰起见,我将它们排除在桌面之外。最奇怪的部分是预算和经验设置完全相同,但不幸的是不保存相同。以下是有关该问题的更多细节。

非常感谢任何有关可能发生的事情的见解。

+------+--------+-----------+------------+-----------------+
| id   | bar_id | day       | budget     | experience      |
+------+--------+-----------+------------+-----------------+
| 1816 |    369 | monday    | LOW BUDGET |                 |
| 1817 |    369 | tuesday   | low budget |                 |
| 1818 |    369 | wednesday | low budget | IN CONVERSATION |
| 1819 |    369 | thursday  | low budget |                 |
| 1820 |    369 | friday    | low budget |                 |
| 1821 |    369 | saturday  | low budget |                 |
+------+--------+-----------+------------+-----------------+

+------+--------+-----------+------------+--------------------+
| id   | bar_id | day       | budget     | experience         |
+------+--------+-----------+------------+--------------------+
| 1822 |    369 | monday    | LOW BUDGET |                    |
| 1823 |    369 | tuesday   | low budget |                    |
| 1824 |    369 | wednesday | low budget |                    |
| 1825 |    369 | thursday  | low budget | ON THE DANCE FLOOR |
| 1826 |    369 | friday    | low budget |                    |
| 1827 |    369 | saturday  | low budget |                    |
+------+--------+-----------+------------+--------------------+

这些是表单(视图)的输入

    <div class="row clearfix">
      <div class="label">Budget</div>
        <div class="mid budget">
        <div class="mid">
          <input class="venProfileInputs caps" name="bar_profile[<%= day %>][budget]" onblur=checkBudVals(this)>
        </div>
      </div>
    </div>

    <div class="row clearfix">
      <div class="label">Experience</div>
        <div class=" mid">
          <input class="venProfileInputs caps" name="bar_profile[<%= day %>][experience]" onblur=checkExVals(this) /> 
        </div>
      </div>

然后它进入控制器

  def update
    @bar = Bar.find(params[:id])
    if @bar.update_attributes(params[:bar])
      @bar.update_profiles(params[:bar_profile])
      redirect_to "/admin", :notice => "Bar was successfully updated."
    else
      render :action => :edit
    end
  end

它在模型中得到更新

  def update_profiles(params)
    profiles.each {|p| p.destroy }

    params[:active_profiles].split(',').each do |profile_day|
      next if profile_day.blank?

      add_profile(params[profile_day.to_sym])

      # sets up crowd tags
      crowd_tags_ids = params[profile_day.to_sym][:tags]
      unless crowd_tags_ids.blank?
        crowd_tags_ids.split(',').each do |tag_id|
          next if tag_id.strip == ""
          profiles.last.crowd_tags << CrowdTag.find(tag_id)
        end
      end      
      profiles.last.set_crowd_tags
      #need to loop through cf_tags_ids = params[profile_day.to_sym][:cf_tags]
      cf_tags_ids = params[profile_day.to_sym][:cf_tags]
      unless cf_tags_ids.blank?
        # loop through each category 0,1,2,3,4
        cf_tags_ids.each_pair{ |cat, ids|
            #cycle through each id in the current cat and add to crowd fiter tag
            ids.split(',').each do |cf_id|
                next if cf_id.strip == ""
                profiles.last.crowd_filter_tags << CrowdFilterTag.find(cf_id)
            end
          }
      end     
      profiles.last.set_cf_tags
      # below is not working for saving experience
      profiles.last.experience = params[profile_day.to_sym][:experience]
      logger.info "#{profiles.last.inspect}"
      #### Still need to save experience some how, budget seems to be getting saved
      # but only one value for experience is being logged in a single row for each bar_id


    end
  end

记录器输出:

Parameters: {"utf8"=>"✓", 
"bar"=>{"name"=>"Rue B", "website"=>"www.ruebnyc.com", "neighbourhood"=>"Greenwich Village",
"street_address"=>"188 Avenue B", "city"=>"New York", "country"=>"US", "state"=>"NY", "zipcode"=>"10009"}, 
"bar_profile"=>{"active_profiles"=>",monday,tuesday,wednesday,thursday,friday,saturday", 

"monday"=>{"day"=>"monday", "description"=>"", "plan_b"=>"", "capacity"=>"100",    
"rating"=>"5", "budget"=>"LOW BUDGET", "experience"=>"ON THE DANCE FLOOR", 
"cf_tags"=>{"0"=>"83,", "1"=>"127,", "2"=>"120,", "3"=>"111,", "4"=>"128,"}, "tags"=>"16,46,45,"}, 

"tuesday"=>{"day"=>"tuesday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"61,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}, 

"wednesday"=>{"day"=>"wednesday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.", 
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"61,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}, 

"thursday"=>{"day"=>"thursday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.", 
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"59,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}, 

"friday"=>{"day"=>"friday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.", 
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"60,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}, 

"saturday"=>{"day"=>"saturday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"59,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}, 

"sunday"=>{"day"=>"sunday", "description"=>"", "plan_b"=>"", "capacity"=>"", "rating"=>"",
"budget"=>"", "experience"=>"", "cf_tags"=>{"0"=>"", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}}, "id"=>"369"}

2 个答案:

答案 0 :(得分:0)

据我所知,它正在做它应该做的事情。您的日志文件显示除“星期一”之外的每个“日”,“体验”设置为“”。星期一,它将设置为“在舞池上”。在哪里 - 因为“预算”每天都被设定为某种东西。

我会查看你的HTML / JS,看看你是否在表单提交之前以某种方式清除该字段。

答案 1 :(得分:0)

我无法想象这对任何人都有帮助,但是在这个庞大的项目中,某些JQuery依赖于html中的特定设置。 “体验”块缺少一个额外的div,允许JS获取正确的值...这就是为什么它一直用nil更新,因为找不到值。

        <div class="row clearfix">
          <div class="label">Budget</div>
          <div class="mid budget">
          <div class="mid">
            <input class="venProfileInputs caps" name="bar_profile[<%= day %>][budget]" onblur=checkBudVals(this)>
            </div>
          </div>
        </div>

        <div class="row clearfix">
          <div class="label">Experience</div>
------>   <div class="mid experience">
          <div class=" mid">
            <input class="venProfileInputs caps" name="bar_profile[<%= day %>][experience]" onblur=checkExVals(this) /> 
          </div>
          </div>
        </div>