0

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 methodeach' for nil:NilClass`

ltname and name is the column name in the table.

4 Answers 4

2

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"])
Sign up to request clarification or add additional context in comments.

2 Comments

the :conditions => { ... } syntax is deprecated.
@simonmenke:- Yes, i know.He's not specify the version of the ruby he's using so gave example of both.Thanks anyway.
1

I'll use the where method to set a condition:

LrTransactionsTable.where(:ltname => "specific word")

Comments

1

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]}

Comments

0

try this:

@entry_ary = LrTransactionsTable.where(ltname: "specific word")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.