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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:03:34+00:00 2026-05-18T04:03:34+00:00

I want to use find_or_create_by, but this statement does NOT work. It does not

  • 0

I want to use find_or_create_by, but this statement does NOT work. It does not “find” or “create” with the other attributes.

productproperty = ProductProperty.find_or_create_by_product_id(:product_id => product.id, :property_id => property.id, :value => d[descname])

There seems to be very little, or no, information on the use of dynamic finders in Rails 3. “and”-ing these together gives me a an unknown method error.

UPDATE:

Originally I couldn’t get the following to work. Please assume I’m not an idiot and “product” is an instance of Product AR model.

product.product_properties.find_or_create_by_property_id_and_value(:property_id => 1, :value => "X")

The error methods was:

no such keys: property_id, value

I couldn’t figure that out. Only this morning did I find the reference to passing the values like this instead:

product.product_properties.find_or_create_by_property_id_and_value(1, "X")

And voilá, it works fine. I would have expected a hash to work in the same situation but I guess not.

So I guess you get a down vote if you miss something on the internet?

  • 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-18T04:03:34+00:00Added an answer on May 18, 2026 at 4:03 am

    If you want to search by multiple attributes, you can use “and” to append them. For example:

    productproperty = ProductProperty.find_or_create_by_product_id_and_property_id_and_value(:product_id => product.id, :property_id => property.id, :value => d[descname])
    

    There is one minor catch to be aware of. It will always return the object you’ve specified, even if that object can’t be saved due to validation errors. So make sure you check to see if the returned object has an id (or is_valid?). Don’t assume its in the database.

    Alternatively, you can use the ‘bang’ version of the method to raise an error if the object cannot be saved:

    http://guides.rubyonrails.org/active_record_querying.html#find-or-create-by-bang

    This applies to Rails 3.

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

Sidebar

Related Questions

Problem Statement: I would like to create an offline database to lookup prices/info on
I've been struggling for quite a while to get this feature working: I want
I want to use WebKit as the layout/rendering engine, and I want to code
So I'm using the excellent Ancestry gem But while the documentation seems very complete
I'm aiming to create a feature in my latest project preferably using PHP. When
I am trying to query the windows desktop search API using SQL. I have
There is a need from a customer to log every data change to a
I am doing regression testing using NUnit, WatiN and VB.net. What i am doing
I've created a Range type: public class Range<T> where T : IComparable<T> { public
I am writing a utility in Java that reads a stream which may contain

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.