You may find this project interesting.
http://drysql.rubyforge.org/features.htm
Paolo
From what i've read, right on the first page they are going the other way I want. Due to the nature of the project i must put the relation in the model, i'm not allowed to modify the database schema.(and the schema does not have de fk specified).
-- Posted via http://www.ruby-forum.com/.
>
Reflecting on associations might get you halfway there:
class Account < AR has_many :users end
Account.reflect_on_association :users
=> #<ActiveRecord::Reflection::AssociationReflection:0x202491c @active_record=Account, @primary_key_name="account_id", @macro=:has_many, @name=:users, @options={}>
Account.reflections will show you all associations
Fire up ./script/console and play around.
Hope this helps.
thank u for the anser, does rails has equivalent for my functions?
# knowing an attribute name i want to know if it is used in a belong to relation
def self.attribute_links_to_a_class?(aatribute) larray = reflect_on_all_associations(:belongs_to) lreturn = false larray.map {|n| if n.primary_key_name.eql?(aatribute);lreturn=true end} lreturn end
Here is a one liner that is simpler.
self.reflect_on_all_associations(:belongs_to).map{ |r| r.primary_key_name }.include?(attr)
# knowing an attribute name i want the linked class to be returned
def self.if_attribute_links_to_a_class_return_class(aatribute) larray = reflect_on_all_associations(:belongs_to) lreturn = nil larray.map {|n| if n.primary_key_name.eql?(aatribute);lreturn=larray.index(n) end} if lreturn return Object.const_get(larray[lreturn].class_name.humanize) else return nil end end
This syntax might be better suited for your second method:
User.reflections[:account].class_name.constantize
Keep in mind that if the relation is of certain types like :through or :polymorphic then the class_name() method will throw an error. You'll need to check the options {} on the reflection:
reflection.options[:polymorphic] || reflection.options[:through]
It might help to play around in ./script/console with your models to check out the varying behaviour.