Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6001345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:49:17+00:00 2026-05-23T00:49:17+00:00

I am trying to write a named scope in a class for Invoice. Invoice

  • 0

I am trying to write a named scope in a class for Invoice. Invoice objects have a link to a Job object. Job objects have a link to a Company object.

class Invoice < ActiveRecord::Base
belongs_to :job

class Job < ActiveRecord::Base
belongs_to :company

I want the scope to be base on whether the ID of the Company related to the Job matches the passed in value.

I am attempting it as something like this

named_scope :job_company, lambda{|job_company_id| {:conditions => {job.company.id => job_company_id}}}

This is giving me an error that says

undefined local variable or method `job’ for #Class:0x103239160

How do I write the lambda function for this?

NB: I am using Ruby 1.8.7 with Rails 2.3.5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:49:18+00:00Added an answer on May 23, 2026 at 12:49 am

    A named_scope is basically just a class method, so inside the lambda, the scope of self is Invoice. There is no job instance or local variable, hence the error.

    But, this should work:

    named_scope :job_company, lambda{|job_company_id| { :joins => { :job => :company }, :conditions => ["jobs.company_id = ?", job_company_id] }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a named scope that will order my 'Products' class based
I am trying to write a named pipe server in C++. I have a
I have three models: Products, Placements, Collections I'm trying to write a name scope
I'm trying write a query to find records which don't have a matching record
Im trying to write something to get my images to show correctly. I have
I'm trying to write a method named isLowerThanFreezing that returns true if the Temperature
I'm trying to write dynamic method that calls Parallel.ForEach. I have checked a IL
Trying to write my first generic class in C#: public class HighScoreList<ScoreType> where ScoreType
I am trying to write a function that will pull the name of a
I'm trying to write a simple Vim function that takes the name of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.