Hi, I''ve been playing with Rails for a few months now and I enjoying it. But most recently, I''ve just come across an issue that I just can''t seem to resolve, to the point where I''ve recreated a small application from scratch to reproduce the issue (and I''m confident the problem is with me not knowing the language well enough). Here''s the issue... I have ACCOUNTS that hold zero or more OPERATIONS. Each OPERATION has a FROM ACCOUNT and a TO ACCOUNT (think accounting). See below for some schema information. Here''s the code for the account.rb model: class Account < ActiveRecord::Base has_many :operations, :order => "actual_date" def has_operations? # # The problem is the "???" what do I replace them with? # :id doesn''t work # @id doesn''t work wither # self.id doesn''t work # if ( Operation.find_by_from_account_id( ??? ).size > 0 || Operation.find_by_to_account_id( ??? ).size > 0 ) return true else return false end end end And here''s my show.rhtml <% for column in Account.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @account.send(column.name) %> </p> <% end %> <% if @account.has_operations? %> <h3>Operations</h3> <table> <tr> <% for operation in @account.expenses %> <td><%= operation.actual_date %></td> <% end %> <td><%= link_to ''Show'', :action => ''show'', :id => operation %></td> <td><%= link_to ''Edit'', :action => ''edit'', :id => operation %></td> <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => operation }, :confirm => ''Are you sure?'', :method => :post %></td> </tr> </table> <% end %> <%= link_to ''Edit this account'', :action => ''edit'', :id => @account %> | <%= link_to ''Return to list'', :action => ''list'' %> Now the above works if I remove the "has_operations?" method from my "account.rb" file AND my Operations table looks like this: create_table "operations", :force => true do |t| t.column "actual_date", :datetime t.column "amount", :decimal t.column "account_id", :integer t.column "to_recipient", :string end *Notice the "account_id" instead of "to_account_id" and "from_account_id". As Rails is nice enough to understand the "has_many" and "belongs_to" relationships. But when I change "account_id" to "from_account_id" and "to_account_id"; create_table "operations", :force => true do |t| t.column "actual_date", :datetime t.column "amount", :decimal t.column "from_account_id", :integer t.column "to_account_id", :integer end the dynamically generated "has_operations?" method shits to bed as it cannot find "account_id" (makes sense). So I replace it (maybe there''s another way) with the version shown above. This new method gets called directly from the "show.rhtml" file and all I need from the instance is the "id" -- which Rails discovers and uses in it''s own "has_operations?" method when it generates it, but I can''t seem to get to with my version... Your help would be much appreciated! Thanks! Marc :o) -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/21/07, Marc Beaudry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, > > I''ve been playing with Rails for a few months now and I enjoying it. But > most recently, I''ve just come across an issue that I just can''t seem to > resolve, to the point where I''ve recreated a small application from > scratch to reproduce the issue (and I''m confident the problem is with me > not knowing the language well enough). > > Here''s the issue... > > I have ACCOUNTS that hold zero or more OPERATIONS. Each OPERATION has a > FROM ACCOUNT and a TO ACCOUNT (think accounting). See below for some > schema information. > > Here''s the code for the account.rb model: > > class Account < ActiveRecord::Base > has_many :operations, :order => "actual_date" > def has_operations? > # > # The problem is the "???" what do I replace them with? > # :id doesn''t work > # @id doesn''t work wither > # self.id doesn''t work > # > if ( Operation.find_by_from_account_id( ??? ).size > 0 || > Operation.find_by_to_account_id( ??? ).size > 0 ) > return true > else > return false > end > end > end > > And here''s my show.rhtml > > <% for column in Account.content_columns %> > <p> > <b><%= column.human_name %>:</b> <%=h @account.send(column.name) %> > </p> > <% end %> > > <% if @account.has_operations? %> > <h3>Operations</h3> > <table> > <tr> > <% for operation in @account.expenses %> > <td><%= operation.actual_date %></td> > <% end %> > <td><%= link_to ''Show'', :action => ''show'', :id => operation > %></td> > <td><%= link_to ''Edit'', :action => ''edit'', :id => operation > %></td> > <td><%= link_to ''Destroy'', { :action => ''destroy'', :id => > operation }, :confirm => ''Are you sure?'', :method => :post %></td> > </tr> > </table> > <% end %> > <%= link_to ''Edit this account'', :action => ''edit'', :id => @account %> | > <%= link_to ''Return to list'', :action => ''list'' %> > > Now the above works if I remove the "has_operations?" method from my > "account.rb" file AND my Operations table looks like this: > > create_table "operations", :force => true do |t| > t.column "actual_date", :datetime > t.column "amount", :decimal > t.column "account_id", :integer > t.column "to_recipient", :string > end > > *Notice the "account_id" instead of "to_account_id" and > "from_account_id". > > As Rails is nice enough to understand the "has_many" and "belongs_to" > relationships. But when I change "account_id" to "from_account_id" and > "to_account_id"; > > create_table "operations", :force => true do |t| > t.column "actual_date", :datetime > t.column "amount", :decimal > t.column "from_account_id", :integer > t.column "to_account_id", :integer > end > > the dynamically generated "has_operations?" method shits to bed as it > cannot find "account_id" (makes sense). So I replace it (maybe there''s > another way) with the version shown above. This new method gets called > directly from the "show.rhtml" file and all I need from the instance is > the "id" -- which Rails discovers and uses in it''s own "has_operations?" > method when it generates it, but I can''t seem to get to with my > version... > > Your help would be much appreciated! > > Thanks! > > Marc :o) > --Marc I believe you''re finding it difficult because your associations aren''t quite setup right. If you want to use from_account_id and to_account_it in operations, this suggests two assocaitions You might model it something like this. class Account < ActiveRecord::Base has_many :operations_from, :class_name => "Operations", :foreign_key => "from_account_id", :order => "actual_date" has_many :operations_to, :class_name => "Operations", :foreign_key => "to_account_id", :order => "actual_date" def has_operations? operations_from.count > 0 || operations_to.count > 0 end end When you''re inside the account model you shouldn;t use Operation.find class methods much if at all. You should use the assocation methods setup by the has_many and belongs to. You should use this version of your migrations for this. create_table "operations", :force => true do |t| t.column "actual_date", :datetime t.column "amount", :decimal t.column "from_account_id", :integer t.column "to_account_id", :integer end You should setup the belongs_to call similarly to the has_many HTH Daniel --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel ----- wrote:> On 8/21/07, Marc Beaudry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Here''s the issue... >> # >> end >> >> %></td> >> >> As Rails is nice enough to understand the "has_many" and "belongs_to" >> the dynamically generated "has_operations?" method shits to bed as it >> >> Marc :o) >> -- > > > > Marc > > I believe you''re finding it difficult because your associations aren''t > quite > setup right. > > If you want to use from_account_id and to_account_it in operations, this > suggests two assocaitions > > You might model it something like this. > > > class Account < ActiveRecord::Base > > has_many :operations_from, :class_name => "Operations", :foreign_key > => > "from_account_id", :order => "actual_date" > has_many :operations_to, :class_name => "Operations", :foreign_key => > "to_account_id", :order => "actual_date" > > def has_operations? > operations_from.count > 0 || operations_to.count > 0 > end > end > > When you''re inside the account model you shouldn;t use Operation.find > class > methods much if at all. You should use the assocation methods setup by > the > has_many and belongs to. > > You should use this version of your migrations for this. > > create_table "operations", :force => true do |t| > t.column "actual_date", :datetime > t.column "amount", :decimal > t.column "from_account_id", :integer > t.column "to_account_id", :integer > end > > > You should setup the belongs_to call similarly to the has_many > > HTH > DanielThanks so much Daniel! :o) My Rails inexperience shows. I coded for 15 years (C, C++, Java) and after a 6 year break -- leading and managing J2EE development teams -- I''ve rediscovered programming through Ruby on Rails and I''m having a blast (too bad I can only spend 3-4 hours a week on this). Have a good one! Marc -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/21/07, Marc Beaudry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Daniel ----- wrote: > > On 8/21/07, Marc Beaudry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> Here''s the issue... > >> # > >> end > >> > >> %></td> > >> > >> As Rails is nice enough to understand the "has_many" and "belongs_to" > >> the dynamically generated "has_operations?" method shits to bed as it > >> > >> Marc :o) > >> -- > > > > > > > > Marc > > > > I believe you''re finding it difficult because your associations aren''t > > quite > > setup right. > > > > If you want to use from_account_id and to_account_it in operations, this > > suggests two assocaitions > > > > You might model it something like this. > > > > > > class Account < ActiveRecord::Base > > > > has_many :operations_from, :class_name => "Operations", :foreign_key > > => > > "from_account_id", :order => "actual_date" > > has_many :operations_to, :class_name => "Operations", :foreign_key => > > "to_account_id", :order => "actual_date" > > > > def has_operations? > > operations_from.count > 0 || operations_to.count > 0 > > end > > end > > > > When you''re inside the account model you shouldn;t use Operation.find > > class > > methods much if at all. You should use the assocation methods setup by > > the > > has_many and belongs to. > > > > You should use this version of your migrations for this. > > > > create_table "operations", :force => true do |t| > > t.column "actual_date", :datetime > > t.column "amount", :decimal > > t.column "from_account_id", :integer > > t.column "to_account_id", :integer > > end > > > > > > You should setup the belongs_to call similarly to the has_many > > > > HTH > > Daniel > > Thanks so much Daniel! :o) > > My Rails inexperience shows. I coded for 15 years (C, C++, Java) and > after a 6 year break -- leading and managing J2EE development teams -- > I''ve rediscovered programming through Ruby on Rails and I''m having a > blast (too bad I can only spend 3-4 hours a week on this). > > Have a good one! > > Marc > --Yeah Ruby''s great. I really like it but I don''t get enough time on it either ;) Glad I could help. -Daniel --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---