在 Rails 3 中从数据库中选择多个选择值时出现问题

发布于 2024-12-16 11:03:10 字数 1408 浏览 2 评论 0原文

非常感谢这里的任何见解。

从 Rails 中的 form_for 内部,我将多个选择值插入到数据库中,如下所示:

<div class="new-partner-form">
<%= form_for [:admin, matching_profile.partner, matching_profile],  :html => {:id => "edit_profile", :multipart => true} do |f| %>
<%= f.submit "Submit", :class => "hidden" %>    
  <div class="rounded-block quarter-wide radio-group">
    <h4>Exclude customers from source:</h4>
     <%= f.select :source, User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, {:include_blank => false}, {:multiple => true} %>
    <%= f.error_message_on :source %>
  </div>

然后我尝试像这样从数据库中提取值:

def does_not_contain_source(matching_profiles)
  Expression.select(matching_profiles, :source) do |keyword|
    Rails.logger.info("Keyword is : " + keyword)
    @customer_source_tokenizer ||= Tokenizer.new(User.select(:source).where("id = ?", self.owner_id).map {|u| u.source}[0]) #User.select("source").where("id = ?", self.owner_id).to_s)
    @customer_source_tokenizer.divergent?(keyword)
  end
end

但是得到这个:

ExpressionErrors: Bad syntax: --- 
- ""
- B
- ""

这就是数据库中的值但当我以这种方式访问​​它时,它似乎感到窒息。这样做的正确方法是什么?

让我这样问吧。用户从多重选择中进行的选择以这种方式插入到数据库字段中:

--- 
- ""
- B
- "

是否有更好、更有用的方法可以将选择的内容存储到数据库中?

ANY insight here is greatly appreciated.

From inside of a form_for in rails, I'm inserting multiple select values into the database, like this:

<div class="new-partner-form">
<%= form_for [:admin, matching_profile.partner, matching_profile],  :html => {:id => "edit_profile", :multipart => true} do |f| %>
<%= f.submit "Submit", :class => "hidden" %>    
  <div class="rounded-block quarter-wide radio-group">
    <h4>Exclude customers from source:</h4>
     <%= f.select :source, User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, {:include_blank => false}, {:multiple => true} %>
    <%= f.error_message_on :source %>
  </div>

I'm then trying to pull the value from the database like this:

def does_not_contain_source(matching_profiles)
  Expression.select(matching_profiles, :source) do |keyword|
    Rails.logger.info("Keyword is : " + keyword)
    @customer_source_tokenizer ||= Tokenizer.new(User.select(:source).where("id = ?", self.owner_id).map {|u| u.source}[0]) #User.select("source").where("id = ?", self.owner_id).to_s)
    @customer_source_tokenizer.divergent?(keyword)
  end
end

but getting this:

ExpressionErrors: Bad syntax: --- 
- ""
- B
- ""

this is what the value is in the database but it seems to choke when i access it this way. What's the right way to do this?

let me ask it this way. The user's selection from the multiple select is being inserted into the db field this way:

--- 
- ""
- B
- "

is there a better, more useful way I can store what's been selected into the DB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-12-23 11:03:10

这有帮助。在我的matching_profiles模型中:

serialize :excluded_sources
serialize :included_sources

this helped. In my matching_profiles model:

serialize :excluded_sources
serialize :included_sources
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文