Hi.
I got a model for banks accounts, each account has many
bank_account_transactions. Each transaction has a from_account_id and a
to_account_id. I''m trying to find all transactions both to and from the
account with a has_many-relation and a conditions-clause.
The account model looks like this:
[code]
has_many :transactions,
:class_name => "BankAccountTransaction",
:conditions => "to_account_id = #{id} or from_account_id =
#{id}"
[/code]
When i try to call the relation trough @account.transactions i get this
error:
[code]
SQLite3::SQLException: no such column:
bank_account_transactions.bank_account_id: SELECT * FROM
"bank_account_transactions" WHERE
("bank_account_transactions".bank_account_id = 1 AND (to_account_id
23229850 or from_account_id = 23229850)) ORDER BY created_at desc
[/code]
The problem here is, that the relation keeps caling the
transaction.bank_account_id, and the model does not have one. How do i
remove the normal foreign_key from the relation?
- Emil
--
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
-~----------~----~----~----~------~----~------~--~---
Not sure if that works, but instead of using :conditions you could use :finder_sql => ... That should tell rails to ignore the default foreign_key --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I have tried this, but I can''t seem to get the account-id into the sql.
If I run this:
[code]
has_many :transactions,
:class_name => "BankAccountTransaction",
:finder_sql => "select * from bank_account_transactions where
bank_account_transactions.to_account_id = #{id} or
bank_account_transactions.from_account_id = #{id}"
[/code]
Then I get an id of a couple of thousands, I suspect it is some internal
id, because, it is definitely not the account id.
How do I get the account id?
Thorsten Mueller wrote:> Not sure if that works, but instead of using :conditions
> you could use :finder_sql => ...
> That should tell rails to ignore the default foreign_key
--
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
-~----------~----~----~----~------~----~------~--~---
> Then I get an id of a couple of thousands, I suspect it is some internal > id, because, it is definitely not the account id. > How do I get the account id?try self.id or self[:id] or read_attribute(:id) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Firstly, thank you for the quick and very good answers. I tried them
all, but with the following results.
This gave an unrealistic hight id-number, that is probably the internal
id again.
[code]
has_many :transactions,
:class_name => "BankAccountTransaction",
:finder_sql => "select * from bank_account_transactions where
bank_account_transactions.to_account_id = #{self.id} or
bank_account_transactions.from_account_id = #{self.id}"
[/code]
This results in an error: expected an array, got nil.
[code]
has_many :transactions,
:class_name => "BankAccountTransaction",
:finder_sql => "select * from bank_account_transactions where
bank_account_transactions.to_account_id = #{self[:id]} or
bank_account_transactions.from_account_id = #{self[:id]}"
[/code]
Results in undefined method
[code]
has_many :transactions,
:class_name => "BankAccountTransaction",
:finder_sql => "select * from bank_account_transactions where
bank_account_transactions.to_account_id = #{read_attribute(:id)} or
bank_account_transactions.from_account_id = #{read_attribute(:id)}"
[/code]
So i really cant seem to find a solution. Do you by any chance have any
other ideas?
Thorsten Mueller wrote:>> Then I get an id of a couple of thousands, I suspect it is some
internal
>> id, because, it is definitely not the account id.
>> How do I get the account id?
>
> try self.id or self[:id] or read_attribute(:id)
--
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 13 Jun 2008, at 12:39, Emil Kampp wrote:> > > Firstly, thank you for the quick and very good answers. I tried them > all, but with the following results. >You need to use single quotes if you want the condition/finder sql to be interpolated at association load time rather than at class load time Fred> This gave an unrealistic hight id-number, that is probably the > internal > id again. > [code] > has_many :transactions, > :class_name => "BankAccountTransaction", > :finder_sql => "select * from bank_account_transactions where > bank_account_transactions.to_account_id = #{self.id} or > bank_account_transactions.from_account_id = #{self.id}" > [/code] > > This results in an error: expected an array, got nil. > [code] > has_many :transactions, > :class_name => "BankAccountTransaction", > :finder_sql => "select * from bank_account_transactions where > bank_account_transactions.to_account_id = #{self[:id]} or > bank_account_transactions.from_account_id = #{self[:id]}" > [/code] > > Results in undefined method > [code] > has_many :transactions, > :class_name => "BankAccountTransaction", > :finder_sql => "select * from bank_account_transactions where > bank_account_transactions.to_account_id = #{read_attribute(:id)} or > bank_account_transactions.from_account_id = #{read_attribute(:id)}" > [/code] > > So i really cant seem to find a solution. Do you by any chance have > any > other ideas? > > > Thorsten Mueller wrote: >>> Then I get an id of a couple of thousands, I suspect it is some >>> internal >>> id, because, it is definitely not the account id. >>> How do I get the account id? >> >> try self.id or self[:id] or read_attribute(:id) > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Nice observation! :) - Emil Frederick Cheung wrote:> On 13 Jun 2008, at 12:39, Emil Kampp wrote: > >> >> >> Firstly, thank you for the quick and very good answers. I tried them >> all, but with the following results. >> > > You need to use single quotes if you want the condition/finder sql to > be interpolated at association load time rather than at class load time > > Fred-- 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 -~----------~----~----~----~------~----~------~--~---