Content Management in rails

Ruchita S. wrote:

Abhi M. wrote:

try this

@content = Content.find (:all,:order => ‘nameof the column DESC’)
eg
if name of column is “name”
@content = Content.find (:all,:order => ‘name DESC’)

Thanks a lot. It worked.

Hi Abhimanyu,

I am implementing the functionality where i have to display the products
details as per user id. I have three tables users table, orders table
and line_items table. The fields are:

users table: id,first_name and so on.
orders table: id, user_id, first_name and so on.
line_items table: id, order_id, product_id, quantity and so on.

Now from these tables i have to display the product_id, quantity… as
per user id. Please tell me how to implement this.

Mislav Marohni�? wrote:

On Nov 8, 10:38 am, Peter B. [email protected] wrote:

Why don’t you take this discussion off-list, or concentrate it to
fewer but longer post? This sort of ping-pong of short messages is not
really helpful to a wider audience.

I must agree with Peter. First, half of this thread is personal
conversation between Abhi and Ruchita and should be taken off this
list. Second, the Rails-related talk is beginning to look more like
chat. You guys should try using IRC and join #rubyonrails on freenode.

Hi,

I need to select the last row from the database. For that am writing the
code:
@sorter = SortingHelper::Sorter.new self, %w(id), params[:id], ‘id’,
‘DESC’
sort_by = params[:sort]
@content = Content.find :all, :order => sort_by, :limit => 1

but this coding is returning the value (#).
Can anyone tell me where i am wrong?

Any help would be greatly appreciated.

Thanks,
Ruchita

one way is to write the like
@order = Order.find_by_user_id(“userid”)->fill the “userid”

@product = Line_items.find_by_order_id(“orderid”)-fill the “orderid”
if there are more than one orders then

@product=Array.new
@orders = Order.find_by_user_id(“userid”)->fill the “userid”
@orders.each do |order|
@var = Line_items.find_by_order_id(order)
@product << @var
end

@product will have all the products that the user have ordered

please send me the code

Abhi M. wrote:

one way is to write the like
@order = Order.find_by_user_id(“userid”)->fill the “userid”

@product = Line_items.find_by_order_id(“orderid”)-fill the “orderid”
if there are more than one orders then

@product=Array.new
@orders = Order.find_by_user_id(“userid”)->fill the “userid”
@orders.each do |order|
@var = Line_items.find_by_order_id(order)
@product << @var
end

@product will have all the products that the user have ordered

it is throwing an error:

NoMethodError in UserController#search

undefined method `each’ for #Order:0x4723338

Abhi M. wrote:

please send me the code

Let me clear my reqt once again. I have three tables users, orders and
line_items. Orders table have user_id and line_items have order_id.
Now the following code is giving error:
uninitialized constant UserController::Line_items

def search
@orders = Order.find :all
session[:query] = params[:query].strip if params[:query]
if session[:query] and request.xhr?
@users = User.find(:all, :conditions => [“first_name LIKE ?”,
“%#{session[:query]}%”], :order => “first_name ASC”)
@product=Array.new
@users.each do |user|
@orders.each do |order|
if user.id == order.user_id
@id = order.id
@var = Line_items.find_by_order_id(@id)
@product << @var
render_text “a”
else
render_text “abc”
end
end
end
end

there are associations between all these tables rt? . also you alsop
want to have all the products of a particular user rt?
please let me know and if possible send the whole error code

On 13 Nov 2007, at 11:38, Ruchita S. wrote:

@orders = Order.find :all
this is horribly inefficient

    @var = Line_items.find_by_order_id(@id)

find_by_x is like a find :first. You probably want find_all_by_x
The class name is LineItem (if you’re adhering to the usual
conventions).
If you’ve defined your relationships properly, you could do something
along the lines of

@product = @users.collect {|u| u.orders.collect(&:line_items)}.flatten

(eager loading those associations might be a good idea)

You seem to be stumbling over some really basic rails stuff. Find a
good book on rails and read and understand it. AWDWR is a popular
favourite, and there’s a book you can get for free at
http://www.sitepoint.com/books/rails1/freebook.php

Fred

Abhi M. wrote:

Hi check whether Line_items is the class name

I have defined all the associations. In line_item model i have defined
belongs_to :order and in order model I have defined belongs_to :user.
The error is:

NameError in UserController#search

uninitialized constant UserController::Line_items

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.3/lib/active_support/dependencies.rb:477:in
const_missing' C:/rails/herbaltea/app/controllers/user_controller.rb:88:insearch’
C:/rails/herbaltea/app/controllers/user_controller.rb:85:in each' C:/rails/herbaltea/app/controllers/user_controller.rb:85:insearch’
C:/rails/herbaltea/app/controllers/user_controller.rb:84:in each' C:/rails/herbaltea/app/controllers/user_controller.rb:84:insearch’

Hi check whether Line_items is the class name

Did u check the class name is it Line_items itself or LineItems or
Line_Items.
Mean while I will check if there is any other errror . do let me know

On 13 Nov 2007, at 12:04, Ruchita S. wrote:

Abhi M. wrote:

Hi check whether Line_items is the class name

I have defined all the associations. In line_item model i have defined
belongs_to :order and in order model I have defined belongs_to :user.
The error is:

NameError in UserController#search

Like I said before, if you are adhering to the conventions, the class
name is LineItem, not line_item or Line_item or Line_items

Fred

Frederick C. wrote:

On 13 Nov 2007, at 12:04, Ruchita S. wrote:

Abhi M. wrote:

Hi check whether Line_items is the class name

I have defined all the associations. In line_item model i have defined
belongs_to :order and in order model I have defined belongs_to :user.
The error is:

NameError in UserController#search

Like I said before, if you are adhering to the conventions, the class
name is LineItem, not line_item or Line_item or Line_items

Fred

Sorry the class name is LineItem.
class LineItem < ActiveRecord::Base
belongs_to :order
end
Even then it is throwing the same error.

ok now send me the code and error once more after u updated

Abhi M. wrote:

ok now send me the code and error once more after u updated

yeah sure.

def search
@orders = Order.find :all
session[:query] = params[:query].strip if params[:query]
if session[:query] and request.xhr?
@users = User.find(:all, :conditions => [“first_name LIKE ?”,
“%#{session[:query]}%”], :order => “first_name ASC”)

  @product=Array.new
  @users.each do |user|
   @orders.each do |order|
  if user.id == order.user_id
      @id = order.id
      @var = Line_items.find_by_order_id(@id)
     @product << @var
     render_text "a"
    else
      render_text "abc"

end
end
end
end
The error is:

NameError in UserController#search

uninitialized constant UserController::Line_items

RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.3/lib/active_support/dependencies.rb:477:in
const_missing' C:/rails/herbaltea/app/controllers/user_controller.rb:88:insearch’
C:/rails/herbaltea/app/controllers/user_controller.rb:85:in each' C:/rails/herbaltea/app/controllers/user_controller.rb:85:insearch’
C:/rails/herbaltea/app/controllers/user_controller.rb:84:in each' C:/rails/herbaltea/app/controllers/user_controller.rb:84:insearch’

Hey you haven’t changed
@var = Line_items.find_by_order_id(@id)

Abhi M. wrote:

Hey you haven’t changed
@var = Line_items.find_by_order_id(@id)

I hve changed it.

what about the error now is it there?

Ruchita S. wrote:

Abhi M. wrote:

Hey you haven’t changed
@var = Line_items.find_by_order_id(@id)

I hve changed it.

Giving the same error… it is giving this error coz there is no
association between users table and lineitems table. Is there any
solution for that?

I hve changed it.

Giving the same error… it is giving this error coz there is no
association between users table and lineitems table. Is there any
solution for that?