我想从与记录模型相关的模型,与模型相关的属性中找到一条记录。
我有一个VendorOrder
模型,并希望通过搜索VendorOrder
来找到一个vendor_order.order.shipments.each {|shipment| shipment.shipping_label.tracking_number}
(或符合条件的vendor_orders)。因此,搜索跟踪号。
型号:
VendorOrder
belongs_to :order
has_many :shipments
Order
has_many :vendor_orders
has_many :shipments
has_many :shipping_labels
Shipment
belongs_to :order
belongs_to :vendor_order
belongs_to :shipping_label
ShippingLabel
belongs_to :order
has_one :shipment
我在控制器中
@vendor_orders = VendorOrder.joins(:order).merge(Order.where(order_status: "paid")).order(created_at: :desc).paginate(page: params[:vendor_page], per_page: 25)
if params[:search]
@vendor_orders = VendorOrder.search(params[:search])
else
@vendor_orders = VendorOrder.joins(:order).merge(Order.where(order_status: "paid")).order(created_at: :desc).paginate(page: params[:vendor_page], per_page: 25)
end
在VendorOrder
模型中,
def self.search(search)
if search
find(:all, :conditions => ['shipments.map {|shipment| shipment.shipping_label.tracking_number} LIKE ?', "%#{search}%"])
else
find(:all)
end
end
查看:
<%= form_tag vendor_orders_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
错误:
Couldn't find all VendorOrders with 'id': (all, {:conditions=>["shipments.map {|shipment| shipment.shipping_label.tracking_number} LIKE ?", "%99999999999999999999%"]}) (found 0 results, but was looking for 2).):
很显然,我并没有完全理解它是如何工作的。到目前为止,我只制作了搜索表单,其中在一个模型内搜索字符串。
如何搜索多层模型,但要确保要搜索的是VendorOrder前端模型?
使用速成帖:
正在做
在模型中:
class << self
def find_by_tracking_number(tracking_number)
joins(:shipments).
where(shipments: {
id: Shipment.find_by(
shipping_label: ShippingLabel.where(tracking_number: tracking_number)
)
})
end
end
控制器:
@vendor_orders = VendorOrder.joins(:order).merge(Order.where(order_status: "paid")).order(created_at: :desc).paginate(page: params[:vendor_page], per_page: 25)
if params[:search]
@vendor_orders = VendorOrder.find_by_tracking_number(params[:search])
else
@vendor_orders = VendorOrder.
joins(:order).
merge(Order.where(order_status: "paid")).
order(created_at: :desc).
paginate(page: params[:vendor_page], per_page: 25)
end
致力于使一切正常运行,但是我遇到了没有结果的问题。
输入数字“ 99999999999999999999”应获得约10个结果,但出现0。
提交时
Parameters: {"utf8"=>"✓", "search"=>"99999999999999999999"}
发挥了魔力:
SELECT "shipments".* FROM "shipments" WHERE "shipments"."shipping_label_id" IN (SELECT "shipping_labels"."id" FROM "shipping_labels" WHERE "shipping_labels"."tracking_number" = $1) LIMIT $2[0m [["tracking_number", "99999999999999999999"], ["LIMIT", 1]]
但是之后什么也没有,它只是呈现页面。
目前,大约有10个VendorOrders的shipments.map { |shpiment| shipment.shipping_label.tracking_number }
==“ 99999999999999999999”
为什么什么也没出现?我的代码中缺少一些内容来追加所有结果吗?
答案 0 :(得分:0)
尝试一下:
<form onSubmit="App.retrieveTransaction();" class="form2">
<h1 class="text-center">Retrieve transactions</h1>
<div class="form-group">
<label for="enterregnoselect">Reg No:</label><input id ="enterregnoselect" class="form-control" type ="text" required/>
</div>
<button type="submit" class="btn btn-primary" >Retrieve</button>
<hr />
<table class="table">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Name</th>
<th scope="col">Item</th>
<th scope="col">Credit</th>
<th scope="col">Debit</th>
<th scope="col">Price</th>
<th scope="col">Qty</th>
</tr>
</thead>
<tbody id="userResults">
</tbody>
</table>
<hr/>
</form>
要进行细分,请假定您的跟踪号位于名为retrieveTransaction: function() {
var transactionInstance;
var enteredregno = $('#enterregnoselect').val();
// var enteredregno = "0015/55";
App.contracts.Payment.deployed().then(function(instance) {
transactionInstance = instance;
return transactionInstance.transactionCount();
}).then(function(transactionCount) {
var userResults = $("#userResults");
userResults.empty();
for (var i = 1; i <= transactionCount; i++) {
transactionInstance.transactions(i).then(function(transaction) {
var date = transaction[0];
var regno = transaction[1];
var name = transaction[2];
var item = transaction[3];
var credit = transaction[4];
var debit = transaction[5];
var price = transaction[6];
var qty = transaction[7];
if(enteredregno == regno) {
var userTemplate = "<tr><th>" + date + "</th><td>" + name + "</td><td>" + item + "</td></tr>" + credit + "</th><td>" + debit + "</td><td>" + price + "</td></tr>" + qty + "</td></tr>";
userResults.append(userTemplate);
}
});
}
});
的变量中。这个:
VendorOrder.
joins(:shipments).
where(shipments: {
id: Shipment.find_by(shipping_label: ShippingLabel.find_by(tracking_number: @tracking_number))
})
...将找到您的@tracking_number
。而且:
ShippingLabel.find_by(tracking_number: @tracking_number)
...将找到ShippingLabel
和Shipment.find_by(shipping_label: ShippingLabel.find_by(tracking_number: @tracking_number))
到Shipment
。
然后,按照指南的Specifying Conditions on the Joined Table部分,从上至上给出最终查询。
您可以在控制器中使用类似
belongs_to
或者,您可以(部分)将其放入ShippingLabel
模型中,例如:
if params[:search]
@vendor_orders = VendorOrder.
joins(:shipments).
where(shipments: {
id: Shipment.find_by(
shipping_label: ShippingLabel.find_by(
tracking_number: params[:search]
)
)
})
else
@vendor_orders = VendorOrder.
joins(:order).
merge(Order.where(order_status: "paid")).
order(created_at: :desc).
paginate(page: params[:vendor_page], per_page: 25)
end
然后您可以在控制器中使用它,例如:
VendorOrder
IMO,这似乎更好一些,并且对控制器隐藏了一些复杂性。