繁体   English   中英

Rails / MySQL仅将一个属性值保存在列中,保存到数据库

[英]Rails/MySQL only one value for attribute is being saved in column, to DB

这将是一个很难解决的问题,因此我将尽我所能。 以下是我想找出的确切问题。 由于某种原因,我保存时,经验是删除列中的所有其他值,并在更新当天将其写入。 我只是保存了所有其他值,但是为了清楚起见,我将它们保留在表之外。 最奇怪的是,预算和经验设置完全相同,但不幸的是没有节省相同的预算。 以下是有关此问题的更多详细信息。

非常感谢您对可能发生的情况的任何见解。

+------+--------+-----------+------------+-----------------+
| 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"}

据我所知,它在做什么。 您的日志文件显示,对于除“星期一”以外的每个“天”,“体验”均设置为“”。 在星期一,它设置为“在舞池上”。 哪里的“预算”每天都设置为某项。

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

我无法想象这真的对任何人都有帮助,但是这个庞大项目中某个地方的某些JQuery依赖于html中的特定设置。 “ experience”块缺少一个额外的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM