RoR:如何只搜索具有特定属性的微博? [英] RoR: how can I search only microposts with a specific attribute?

查看:125
本文介绍了RoR:如何只搜索具有特定属性的微博?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在我拥有的app/views/microposts/home.html.erb中.

<% form_tag purchases_path, :method => 'get', :id => "products_search" do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %>

<% form_tag sales_path, :method => 'get', :id => "sales_search" do %>
      <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil %>
      </p>
    <% end %>

然后在micropost.rb中,我有

scope :purchases, where(:kind => "purchase")
  scope :sales, where(:kind => "sale")

 def self.search(search)
    if search
      where('name LIKE ?', "%#{search}%")
    else
      scoped
    end
  end

,最后在microposts_controller.rb中,我拥有

    def home

    @microposts=Micropost.all
    @purchases=@microposts.collect{ |m| m if m.kind == "purchase"}.compact
    @sales=@microposts.collect{ |m| m if m.kind == "sale"}.compact

  end

我也尝试使用

def home

    @microposts=Micropost.all
    @purchases=@microposts.purchases
    @sales=@microposts.sales

  end

相反,但是它给了我##pp的错误未定义方法购买"

没关系,

现在,使用.collect方法时,出现一个错误,提示未定义的局部变量或方法"purchases_path",它对sales_path的作用相同.

我想要的是拥有两个搜索表单.在我的微型帖子表中,我有一列名为kind的列,可以作为购买"或销售".如何更改代码,以便一个搜索表单可以搜索并仅显示具有购买"类型的微博的结果.然后其他搜索仅搜索并显示销售"类型的那些微博的结果.

解决方案

为什么不将微职位拆分为两个继承微职位模型的新模型?

class Micropost < ActiveRecord::Base

  #do your general micropost magic here

end

下一步:

class Purchase < Micropost

  #do your purchase specific magic here

end

并且:

class Sale < Micropost

  #do your sale specific magic here

end

您的家庭活动:

def home

  @microposts = Micropost.all
  @purchases = Purchase.all
  @sales = Sale.all

end

如果您需要搜索购买":

Purchase.where(:something => "great")

如果您需要搜索销售:

Sale.where(:something => "greater")

如果您需要同时搜索:

Micropost.where(:something => "the greatest")

**注意**您需要在microposts表中添加一列类型"才能起作用,更多信息:单个表继承@

and then in micropost.rb I have

scope :purchases, where(:kind => "purchase")
  scope :sales, where(:kind => "sale")

 def self.search(search)
    if search
      where('name LIKE ?', "%#{search}%")
    else
      scoped
    end
  end

and then finally in the microposts_controller.rb I have

    def home

    @microposts=Micropost.all
    @purchases=@microposts.collect{ |m| m if m.kind == "purchase"}.compact
    @sales=@microposts.collect{ |m| m if m.kind == "sale"}.compact

  end

edit:

I also tried using

def home

    @microposts=Micropost.all
    @purchases=@microposts.purchases
    @sales=@microposts.sales

  end

instead but then it gives me the error undefined method `purchases' for #

Anways,

Right now with the .collect method I am getting an error saying undefined local variable or method `purchases_path' and it does the same for sales_path.

What I want is to have TWO search forms. In my micropost table I have a column called kind which can be either "purchase" or "sale". How can I change my code so that one search form searches through and displays results for only those microposts with the kind "purchase". And then the other searches through and displays results for only those microposts with the kind "sale"

解决方案

Why dont you split the microposts in two new models inheriting the micropost model?

class Micropost < ActiveRecord::Base

  #do your general micropost magic here

end

Next:

class Purchase < Micropost

  #do your purchase specific magic here

end

And:

class Sale < Micropost

  #do your sale specific magic here

end

Your home action:

def home

  @microposts = Micropost.all
  @purchases = Purchase.all
  @sales = Sale.all

end

If you need to search Purchases:

Purchase.where(:something => "great")

If you need to search Sales:

Sale.where(:something => "greater")

If you need to search both:

Micropost.where(:something => "the greatest")

** NOTE ** You need to add a column "type" to the microposts table for this to work, more information: Single table inheritance @ http://api.rubyonrails.org/classes/ActiveRecord/Base.html

这篇关于RoR:如何只搜索具有特定属性的微博?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆