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

  • SEARCH
  • Home
  • 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 6137277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:42:21+00:00 2026-05-23T17:42:21+00:00

class Message has_many :threads, :class_name=>Message, :conditions => `#{Message.table_name}`.conversation_id = #{self.send(:conversation_id)} end m = Message.first

  • 0
class Message  
  has_many   :threads,  :class_name=>"Message", :conditions => "`#{Message.table_name}`.conversation_id = #{self.send(:conversation_id)}"  
end  

m = Message.first  
NoMethodError: undefined method `conversation_id' for #<Class:0xc5021dc>  

I even tried with single quote:

class Message  
  has_many   :threads,  :class_name=>"Message", :conditions => '`#{Message.table_name}`.conversation_id = #{self.send(:conversation_id)}'  
end  

m = Message.first  
m.threads  

This gave me Mysql::Error: You have an error in your SQL syntax
It seems it’s not considering the #{...} thing while generating the condition sql

i could do it with scopes
scope :threads, lambda {|conv_id| where(:conversation_id => conv_id) }
and access it Message.where(“some condition”).threads()
but am looking for a neat association like
m = Message.find(1000)
m.threads should give all the conversation threads which it belongs to

  • 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-23T17:42:22+00:00Added an answer on May 23, 2026 at 5:42 pm

    You cannot use dynamic conditions in has_many. However, in your particular case it seems you need primary_key and foreign_key instead:

    class Message  
      has_many :threads, :class_name=>"Message", :primary_key => 'conversation_id', :foreign_key => 'conversation_id'
    end
    

    You may also be interested by one of the gems that adds tree structure to ActiveRecord.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
How would I declare a class that has a method/message that takes a selector,
I am sending emails with the following method: class Communicate < ActionMailer::Base def message(sub,msg,people)
Three models: class Customer < ActiveRecord::Base has_many :visits end class Visit < ActiveRecord::Base belongs_to
class Person has_many :owned_groups, :class_name => Group, :foreign_key => :owner_id has_many :owned_group_memberships, :through =>
I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>
I have 4 models, Message, Group, User, Membership class Group < ActiveRecord::Base has_many :memberships
class Message(models.Model): subject = models.CharField(max_length=100) pub_date = models.DateTimeField(default=datetime.now()) class Topic(models.Model): title = models.CharField(max_length=100) message
Suppose the following class public class Message { // some code } And 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.