Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect the :conditions option into associations #61

Closed
sobrinho opened this issue Oct 1, 2009 · 9 comments
Closed

Reflect the :conditions option into associations #61

sobrinho opened this issue Oct 1, 2009 · 9 comments

Comments

@sobrinho
Copy link
Member

sobrinho commented Oct 1, 2009

Hello,

I would to request a new feature, reflect the :conditions options into associations. I have a application like that:

class Company < ActiveRecord::Base
  validates_inclusion_of :company_type, :in => %w(group_company customer)
end

class Case < ActiveRecord::Base
  belongs_to :customer, :class_name => 'Company', :conditions => { 'companies.company_type' => 'customer' }
  belongs_to :company, :conditions => { 'companies.company_type' => 'group_company' }
end

And I created a form like that (using HAML):

      - semantic_form_for Case.new do |f|
        - f.inputs do
          = f.input :company, :required => false, :prompt => '- Companies'
          = f.input :customer, :required => false, :prompt => '- Customers'

        - f.buttons do
          = f.submit

But formtastic doesn't reflect the conditions. Well, I arounded that with this:

      - semantic_form_for Case.new do |f|
        - f.inputs do
          = f.input :company, :required => false, :collection => Company.all(:conditions => { :company_type => 'group_company' } }), :prompt => '- Companies'
          = f.input :customer, :required => false, :collection => Company.all(:conditions => { :company_type => 'customer' } }),, :prompt => '- Customers'

        - f.buttons do
          = f.submit

I think that is a great feature and will be very apreciated by users :)

@justinfrench
Copy link
Member

Sounds like a good idea!

@grimen
Copy link
Contributor

grimen commented Oct 5, 2009

I guess this is low prio - I never seen one IRL use :conditions on associations, it's really not that common. If it would be a feature, I would suggest to make ValidationReflection plugin more generic...and as I have done some work on ValidationReflection lately, I can say you only have to change a few lines - making it a "ModelReflection" quite easy.

@grimen
Copy link
Contributor

grimen commented Oct 5, 2009

Oh...my bad, no need extend ValidationReflection - everything that's needed is in Rails core:

http://github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb

@anshul
Copy link

anshul commented Apr 2, 2010

Just wanted to add +1 to this feature request... It is natural to expect an association's input to honour the association's conditions.

@sobrinho
Copy link
Member Author

sobrinho commented Apr 2, 2010

I made a initializer for my project: http://gist.github.com/353414

With a good refactoring can be merged on core :)

@justinfrench
Copy link
Member

Patch with spec coverage welcome!

@justinfrench
Copy link
Member

Sobrinho, should this make it into 1.0, or can it wait?

@sobrinho
Copy link
Member Author

I will do this for 1.0. I think this is important.

I will make the patch with specs on master branch tomorrow.

@sobrinho
Copy link
Member Author

find_raw_collection_for_column should honour the association's conditions

Closed by b5f771a

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants