I have a general question about Rails controller or instance method. Say, I have two models, Give Take Each of these has an identical set of columns like, Give : weight:integer, day:date Take: weight:integer, day:date When @give = Give.new is created, I want to search if there is a counterpart, Take, having the same values, weight and day. So I need to define a search method somewhere. I believe it will look like @give = Give.new ... if @give.search_counter #<- returns true if the counterpart exists ... else end This must work for Take as well @take = Take.new ... if @take.search_counter #<- returns true if the counterpart exists ... else end Questions: Is it possible? I am not sure if the method works without arguments... Where do I have to define the method? application_controller.rb maybe? soichi -- 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 https://groups.google.com/groups/opt_out.
On 11 November 2012 06:31, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a general question about Rails controller or instance method. > > > Say, I have two models, > > Give > Take > > Each of these has an identical set of columns like, > > Give : weight:integer, day:date > Take: weight:integer, day:date > > When @give = Give.new is created, I want to search if there is a > counterpart, Take, having the same values, weight and day. So I need to > define a search method somewhere. > > I believe it will look like > > @give = Give.new > ... > if @give.search_counter #<- returns true if the counterpart exists > ... > else > end > > This must work for Take as well > > @take = Take.new > ... > if @take.search_counter #<- returns true if the counterpart exists > ... > else > end > > > Questions: > Is it possible? I am not sure if the method works without arguments... > Where do I have to define the method? application_controller.rb maybe?It looks to me as if you should only have one table for both give and take. Is there a reason why this is not possible. Colin -- 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 https://groups.google.com/groups/opt_out.
> > It looks to me as if you should only have one table for both give and > take. Is there a reason why this is not possible.You might be right. I will try that way. Thanks. soichi -- 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 https://groups.google.com/groups/opt_out.
On 11 November 2012 09:24, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> >> It looks to me as if you should only have one table for both give and >> take. Is there a reason why this is not possible. > > > > You might be right. I will try that way. > Thanks.Note that you can have multiple associations into the same table using and :class_name option. So for example class Something belongs_to :give, :foreign_key => "give_id", :classname => "Widget" belongs_to :take, :foreign_key => "take_id", :classname => "Widget" Have a look at the Rails Guide on ActiveRecord Associations for more information. Colin -- 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 https://groups.google.com/groups/opt_out.
Soichi Ishida
2012-Nov-11 10:09 UTC
Re: Re: help for ideas of instance method or controller
> Note that you can have multiple associations into the same table using > and :class_name option. So for example > class Something > belongs_to :give, :foreign_key => "give_id", :classname => "Widget" > belongs_to :take, :foreign_key => "take_id", :classname => "Widget" > > Have a look at the Rails Guide on ActiveRecord Associations for more > information.Thanks. I always wonder why I do things more complicated than necessary... ;) soichi -- 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 https://groups.google.com/groups/opt_out.