Reputation: 2821
Thanks for your time !
It's a simple question but I've searched for some time and still cannot get the function suit me.
I get a table named *lr_transaction_tables* in DB and a class LrTransactionsTable < ActiveRecord::Base
with it.
I can @entry_ary = LrTransactionsTable.find(:all)
to fetch the whole table and present it in the view by :
<table id="trans-war-table">
<% @entry_ary.each do |item| %>
<tr>
<td><%= item.ltname %></td>
<td><%= item.name %></td>
</tr>
<% end %>
</table>
But how do I get the data where the data under ltname column is "specific word"
instead of all of it.
I've tried @entry_ary = LrTransactionsTable.find_by_ltname( "specific word" )
, but it give me the error that undefined method
each' for nil:NilClass`
ltname
and name
is the column name in the table.
Upvotes: 0
Views: 96
Reputation: 6036
try best way, always right query in model
in your controller
LrTransactionsTable.find_ltname("specific word")
and write in your model
def find_ltname(name)
where("ltname = ?", name)
end
or you can also create a scope for that
scope :find_ltname, lambda{|name|{ :conditions => ["ltname = ?", name]}
Upvotes: 1
Reputation: 703
I'll use the where
method to set a condition:
LrTransactionsTable.where(:ltname => "specific word")
Upvotes: 1
Reputation: 2880
try this:
@entry_ary = LrTransactionsTable.where(ltname: "specific word")
Upvotes: 0
Reputation: 47542
For Rails 2
LrTransactionsTable.find(:all, :conditions => {:ltname => "specific word"})
For Rails 3
@entry_ary = LrTransactionsTable.where(ltname: "specific word")
OR
@entry_ary = LrTransactionsTable.where(["ltname =? ", "specific word"])
Upvotes: 2