Hi all,
I am currently trying to get a bunch of links to to display all the
related console records upon button click so e.g. If a user clicks on
the Dreamcast link then I would like it to display all records that have
Dreamcast stored as their console.
I have the following example code in my index class:
<%= link_to ''DREAMCAST'', games_path(:console =>
''Dreamcast'')%>
the above code is repeating for each individual console.
Now in my gamescontroller I have the following in my index:
def index
@games = Game.paginate(:per_page => 4, :page =>
params[:page]).search(params[:search])
@games = @games.find_by_console(params[:console]) unless
params[:console].blank?
end
The problem I receive is that when I click on the Gaming section of my
website I receive the following error message which I assume is to say
activerecord is not connecting:
undefined method `find_by_console'' for
#<WillPaginate::Collection:0x52cb9d8>
How would I go about solving this problem I have come across?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23.02.2012, at 22:23, Roger Patrick wrote:> Now in my gamescontroller I have the following in my index: > > def index > @games = Game.paginate(:per_page => 4, :page => > params[:page]).search(params[:search]) > @games = @games.find_by_console(params[:console]) unless > params[:console].blank? > end > > The problem I receive is that when I click on the Gaming section of my > website I receive the following error message which I assume is to say > activerecord is not connecting: > > undefined method `find_by_console'' for > #<WillPaginate::Collection:0x52cb9d8>At first, .find_by_<attr_name> is a ActiveRecord::Relation''s method. That is why it doesn''t responded. At second, this method is a "lazy" method, oriented to return _just_one_ instance, corresponding to condition. Is that your app-logic? I don''t think so.. So, your way is: games_relation = case params[:console].present? when true then Game.where(:console => params[:console]) else Game end @games = games_relation.paginate(:per_page => 4, :page => params[:page]).search(params[:search]) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Valery Kvon wrote in post #1048505:> On 23.02.2012, at 22:23, Roger Patrick wrote: > >> website I receive the following error message which I assume is to say >> activerecord is not connecting: >> >> undefined method `find_by_console'' for >> #<WillPaginate::Collection:0x52cb9d8> > > At first, .find_by_<attr_name> is a ActiveRecord::Relation''s method. > That is why it doesn''t responded. > At second, this method is a "lazy" method, oriented to return _just_one_ > instance, corresponding to condition. Is that your app-logic? I don''t > think so.. > > So, your way is: > > games_relation = case params[:console].present? > when true then Game.where(:console => params[:console]) > else Game > end > @games = games_relation.paginate(:per_page => 4, :page => > params[:page]).search(params[:search])Ok thanks for your feedback, so of what I have understood in my gamescontroller.rb I will have the following: def index @games = games_relation.paginate(:per_page => 4, :page => params[:page]).search(params[:search]) end in my game.rb file I will have the following: games_relation = case params[:console].present? when true then Game.where(:console => params[:console]) else Game end and in my index.html.erb I will have the following: <%= link_to ''DREAMCAST'', games_path(:console => ''Dreamcast'')%> please correct me if I am wrong. Thanks for your help. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23.02.2012, at 23:15, Roger Patrick wrote:>> > def index > @games = games_relation.paginate(:per_page => 4, :page => > params[:page]).search(params[:search]) > end > > in my game.rb file I will have the following: > > games_relation = case params[:console].present? > when true then Game.where(:console => params[:console]) > else Game > end > > please correct me if I am wrong. Thanks for your help.No no no, that was all for index action: def index games_relation = case params[:console].present? when true then Game.where(:console => params[:console]) else Game end @games = games_relation.paginate(:per_page => 4, :page => params[:page]).search(params[:search]) end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> def index > games_relation = case params[:console].present? > when true then Game.where(:console => params[:console]) > else Game > end > @games = games_relation.paginate(:per_page => 4, :page => > params[:page]).search(params[:search]) > endThank you very much, that has solved my problem :) Thanks for all your help you have saved me many of hours pondering. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.