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 8467777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:44:34+00:00 2026-06-10T15:44:34+00:00

I have my model defined like this: class Animal < ActiveRecord::Base mount_uploader :picture, PictureUploader

  • 0

I have my model defined like this:

class Animal < ActiveRecord::Base
    mount_uploader :picture, PictureUploader

    attr_accessible :picture, :born_date, :father_id, :mother_id, :name, :obs, :earring, :animal_type, :animal_type_id, :inseminations

    validates :name, :born_date, :presence => true
    validates :earring, :presence => true, :if => :should_have_earring?

    belongs_to :father, :class_name => "Animal"
    belongs_to :mother, :class_name => "Animal"
    belongs_to :animal_type
    has_one :birth

    has_one :sell
    has_one :death

    has_many :inseminations
end

and

class Insemination < ActiveRecord::Base
  attr_accessible :bull_id, :cow_id, :done, :expected_birth_date, :expected_dry_date, :insemination_date, :obs, :rut

  validates :bull_id, :presence => true
  validates :cow_id, :presence => true
  validates :insemination_date, :presence => true

  belongs_to :bull, :class_name => "Animal"
  belongs_to :cow, :class_name => "Animal"

  has_one :birth
  has_one :abortion
  has_one :dry
end

Good, somewhere, I want to get the last insemination from some animal… so, I do @animal.inseminations.last, it should work, but, it does the select using a animal_id property, that does not exist in insemination model. So I get an error like this:

Mysql2::Error: Unknown column ‘inseminations.animal_id’ in ‘where
clause’: SELECT inseminations.* FROM inseminations WHERE
inseminations.animal_id = 1 ORDER BY inseminations.id DESC
LIMIT 1

How can I specify it to searh in cow_id and/or bull_id columns? Is that possible?

Thanks in advance.

  • 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-06-10T15:44:36+00:00Added an answer on June 10, 2026 at 3:44 pm

    I think you have a few different options:

    1) Instead of using has_many :inseminations, create two separate has many relationships:

    has_many :cow_inseminations, :class_name => 'Insemination', :foreign_key => 'cow_id'
    has_many :bull_inseminations, :class_name => 'Insemination', :foreign_key => 'bull_id'
    

    2) Use STI and create subclasses of Animal. You will need to add a type field to Animal for this to work:

    class Cow < Animal
      has_many :inseminations, :foreign_key => 'cow_id'
    end
    
    class Bull < Animal
      has_many :inseminations, :foreign_key => 'bull_id'
    end
    

    Then you can do Bull.first.inseminations or Cow.first.inseminations

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

Sidebar

Related Questions

I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
So I have defined a model like this: class Model extends Zend_Db_Table_Abstract { $_primary
I have a model defined like this: class UserDetail(models.Model): user = models.ForeignKey(User, db_index=True, unique=True,
I have a model like this defined... public class Product{ private long timestamp; //
I have a before_save in my Message model defined like this: class Message <
Within my model I have defined a required field class like this: class Contact(models.Model):
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have three models defined as follows: class Comic < ActiveRecord::Base has_many :feeds end
I have a Link and a Bookmark model like this: class Link(models.Model): url =

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.