简体   繁体   中英

Accessing rails helper methods in models

In Rails 3, I am having a problem accessing a helper method from within a model

In my ApplicationController I have a helper method called current_account which returns the account associated with the currently logged in user. I have a project model which contains projects that are associated with that account. I have specified 'belongs_to :account' in my project model and 'has_many' in my account model. All of this seems to be working correctly and my projects are being associated with the right accounts.

However at the moment when I use 'Project.all', all of the project records are being returned as you would expect. I would like it to automatically filter the projects so that only those associated with the specified account are returned and I would like this to be the default behaviour

So I tried using 'default_scope'. The line in my model looks something like this

default_scope :order => :name, :conditions => ['account_id = ?', current_account.id]

This throws an error saying

Undefined local variable or method current_account for #

If I swap the call to current_account.id for an integer id - eg

default_scope :order => :name, :conditions => ['account_id = ?', 1]

Everything works correctly. How do I make my current_account method accessible to my model

Many Thanks

You can't access the session from models. Instead, pass the account as a parameter to a named scope.

#controller
Model.my_custom_find current_account

#model.rb

named_scope :my_custom_find, lambda { |account| { :order => :name, :conditions => ['account_id = ?', account.id]}}

I haven't used rails 3 yet so maybe named_scopes have changed.

The association setup is enough to deal with scoping on controller and view levels. I think the problem is to scope, for instance, finds in models.

In controller:

@products = current_account.products.all

Fine for view scoping, but...

In model:

class Inventory < ActiveRecord::Base
  belongs_to :account # has fk account_id
  has_many :inventory_items, :dependent => :destroy
  has_many :products, :through => :inventory_items
end

class Product < ActiveRecord::Base
  has_many :inventory_items

  def level_in(inventory_id)
    inventory_items.where(:inventory_id => inventory_id).size
  end

  def total_level
    # Inventory.where(:account_id => ?????) <<<<<< ISSUE HERE!!!! 
    Inventory.sum { |inventory| level_in(inventory.id) }
  end
end

How can this be scoped?

+1 for mark's answer. This should still work in Rails 3.

Just showing you the rails 3 way with scope and new query api:

scope :my_custom_find, lambda { |account| where(:account_id=>account.id).order(:name) }

You've got the association set up, so couldn't you just use:

@projects = current_account.projects.all

...in your controller?

I've not adjusted the syntax to be Rails 3 style as I'm still learning it myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM