Giter Site home page Giter Site logo

active_hash's People

Contributors

adampal avatar chrisbr avatar djberg96 avatar flavorjones avatar grosser avatar iberianpig avatar ise-tang avatar jmonteiro avatar kbrock avatar machisuke avatar mattheworiordan avatar nowaker avatar nsommer avatar okuramasafumi avatar pfeiffer avatar rheaton avatar sean0628 avatar stomk avatar syguer avatar tamgrosser avatar tbotaq avatar tomstuart avatar ugomare avatar usernam3 avatar vandrijevik avatar y-yagi avatar yhirano55 avatar ytkg avatar yujideveloper avatar zilkey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

active_hash's Issues

Cannot have non-english characters in hash data.

I am trying to store Country with two languages. So I have :id, :name, :name_ch for every country. I found that if I use English characters for :name_ch, it let me access either name or name_ch with no problem. If I use Chinese character for :name_ch, it throws this error.

Syntax error, unexpected $end, expecting '}'
{:id => 1, :name => "China",:name_ch=>"**"},
Is there a fix for this? I think it is best if I can keep the translations in the hash data instead of having to declare them in i18n yml file.

make `belongs_to` work again on AR?

class Country < ActiveHash::Base
end

class City < ActiveRecord::Base
  belongs_to :country # => this doesn't work, it needs to be...
  belongs_to_active_hash :country
end

I'm thinking to make this work, we could overwrite belongs_to on ActiveRecord::Base, make it check to see if we are attempting to belong_to an ActiveHash object, and if so, just call belongs_to_active_hash.

save() should add the object to class.data and auto-generate an ID

That way we could do :

foo = Foo.new :size => 'large', :bitchinness => 'very'
foo.save

and then we could make Fixjour just work.

This also implies that new_record? should do the right thing and examine .data array.

Also:

  • save!
  • create
  • create!

Peter will help you with this

How do I reload a YAML?

If there are only changes in the yml data, (such as no changes in model.rb)
how do I force a reload?

Compatibility with Rails 4.0

Just a quick reminder, this Gem isn't currently compatible with Rails 4.0. Trying to run it with Rails 4.0.0.beta1 will result in:

/(...)/activerecord-4.0.0.beta1/lib/active_record/reflection.rb:20:in `create_reflection': wrong number of arguments (4 for 5) (ArgumentError)
    from /(...)/active_hash-ca2e9b625bce/lib/associations/associations.rb:32:in `belongs_to_active_hash'

That's because ActiveRecord::Reflection::ClassMethods.create_reflection now receives a new 3rd argument, scope: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb#L20

interrogator methods do the wrong thing

  def define_interrogator_method(field)
    method_name = "#{field}?"
    unless instance_methods.include?(method_name)
      define_method(method_name) do
        attributes[field].present?
      end
    end
  end

how about just returning the boolean of the field like activerecord does?

:attributes as field name fails spectacularly

Adding field :attributes causes an immediate SystemStackError: stack level too deep with one (useless) level of backtrace. (When running Cucumber it's somewhere in Cucumber; when running RSpec it's somewhere in RSpec.)

Probably the right thing to do is to raise a better error sooner, so the user knows what the real problem is.

Warning for non-id primary key when creating association from ActiveRecord

I have an ActiveRecord model with a belongs_to association to an ActiveHash, which uses the :name field instead of :id for foreign key. When looking up the association, ruby complains with:

var/lib/gems/1.8/gems/active_hash-0.8.4/lib/active_hash/base.rb:201: warning: multiple values for a block parameter (2 for 1)
from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/belongs_to_association.rb:49

It looks like ActiveRecord wants to pass some options to +find_by_name+, but the function as defined takes only one argument. Here's one way to fix it (in ActiveHash::Base):

   def define_custom_find_method(field_name)
    method_name = "find_by_#{field_name}"
    unless singleton_methods.include?(method_name)
      the_meta_class.instance_eval do
        define_method(method_name) do |*args|
          options = args.extract_options!
          name = args[0]
          all.detect { |record| record.send(field_name) == name }
        end
      end
    end
  end

By the way, here are the options being sent:
["Visa", {:include=>nil, :select=>nil, :readonly=>nil, :conditions=>nil}]

Undefined method `scoped' with has many through

Hello, I have the following models:

class User < ActiveRecord::Base
  has_many :talks
  has_many :languages, { through: :talks }
end
class Talk < ActiveRecord::Base
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :language
  belongs_to :user
end
class Language < ActiveYaml::Base
  include ActiveHash::Associations
  has_many :talks
end

And everything's working perfectly. For instance:

@user = User.first #=> #<User id: 168, (...)>
@user.talks        #=> [#<Talk id: 1, language_id: 1, user_id: 168, (...)>, (...)]

Except when:

@user.languages
(Object doesn't support #inspect)

However, this works well:

@user.talks.first.language #=> #<Language:0x007fcc8c298ac8 (...)>

I also have this backtrace from an rspec test:

(rdb:1) eval @current_user.talks
[#<Talk id: 1, language_id: 1, user_id: 1, level: nil, created_at: "2012-09-11 13:12:09", updated_at: "2012-09-11 13:12:09">]
(rdb:1) eval @current_user.talks.class
Array
(rdb:1) eval @current_user.languages
INTERNAL ERROR!!! undefined method `scoped' for Language:Class
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/active_hash-0.9.12/lib/active_hash/base.rb:218:in `method_missing'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/association.rb:123:in `target_scope'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/through_association.rb:15:in `target_scope'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/association.rb:87:in `scoped'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/has_many_through_association.rb:173:in `find_target'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:333:in `load_target'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/commands/eval.rb:47:in `block in execute'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/commands/eval.rb:19:in `run_with_binding'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/commands/eval.rb:46:in `execute'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:275:in `one_cmd'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:261:in `block (2 levels) in process_commands'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:260:in `each'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:260:in `block in process_commands'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:253:in `catch'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:253:in `process_commands'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug/processor.rb:173:in `at_line'
    (eval):5:in `block in at_line'
    <internal:prelude>:10:in `synchronize'
    (eval):3:in `at_line'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/debugger-1.2.0/lib/ruby-debug-base.rb:54:in `at_line'
    /Users/cyril/code/Bonjour-Bonjour/spec/controllers/api/v1/private/my/users_controller_spec.rb:16:in `block (3 levels) in <top (required)>'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:236:in `instance_eval'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:236:in `instance_eval'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/hooks.rb:23:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/hooks.rb:72:in `block in run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/hooks.rb:72:in `each'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/hooks.rb:72:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/hooks.rb:424:in `run_hook'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:323:in `run_before_each_hooks'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:299:in `run_before_each'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:112:in `block in run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:253:in `with_around_each_hooks'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:110:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:378:in `block in run_examples'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `map'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `run_examples'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:360:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `block in run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `map'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `map'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `block in run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:34:in `report'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
    /Users/cyril/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'F/Users/cyril/code/Bonjour-Bonjour/spec/controllers/api/v1/private/my/users_controller_spec.rb:16

Thanks for your awesome work!

find_by_something doesn't seem to work until after I've called a find(_by_id)

While find(_by_id) works fine. If I try to find by another attribute it fails until after I've run a find(_by_id).

This is concerning ActiveYaml

first it fails, then it works
ex:

TouchType.find_by_name "something"
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
from /opt/local/lib/ruby/gems/1.8/gems/active_hash-0.9.1/lib/active_hash/base.rb:139:in respond_to?' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:380:inall?'
from /opt/local/lib/ruby/gems/1.8/gems/active_hash-0.9.1/lib/active_hash/base.rb:138:in each' from /opt/local/lib/ruby/gems/1.8/gems/active_hash-0.9.1/lib/active_hash/base.rb:138:inall?'
from /opt/local/lib/ruby/gems/1.8/gems/active_hash-0.9.1/lib/active_hash/base.rb:138:in respond_to?' from /opt/local/lib/ruby/gems/1.8/gems/active_hash-0.9.1/lib/active_hash/base.rb:145:inmethod_missing'
from (irb):1
TouchType.find 1
=> #<TouchType:0x10367fbc0 @attributes={:name=>"something", :id=>1}>
TouchType.find_by_name "something"
=> #<TouchType:0x10367fbc0 @attributes={:name=>"something", :id=>1}>

has_many through invalid

hello, friend, I encountered a problem 'active_hash', eg:

class Template < ActiveHash::Base

include ActiveHash::Associations

has_many :job_type_templates, dependent: :destroy
has_many :job_types, through: :job_type_templates

self.data = [
.......
]

end

Template.first.job_types "ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'job_types.template_id'"

Template.first.job_type_templates Correct

I should use eg:

def job_types
......
end

but don't support job_type_ids, thank you!

undefined method `scoped'

Hi,

I'm developing a Rails 3.1.1 application. An ActiveRecord model belongs to an ActiveHash model. When I try to follow the relation in this way (AR -> AH), I get the following error:
undefined method 'scoped'

I hope this could help you,
Camille

Cannot install version 1.2.1 with Rails 4.

Released active_hash gem version 1.2.1 depends on activesupport < 4, >= 2.2.2 (https://rubygems.org/gems/active_hash/versions/1.2.1) while version 1.2.0 only depends on activesupport >= 2.2.2 (https://rubygems.org/gems/active_hash/versions/1.2.0)

If I have in Gemfile:

ruby '2.0.0'
gem 'rails', '~> 4.0.1'
gem 'active_hash', '~> 1.2.1'

Then I get this error when run bundle update

Bundler could not find compatible versions for gem "activesupport":
  In Gemfile:
    active_hash (~> 1.2.1) ruby depends on
      activesupport (< 4, >= 2.2.2) ruby

    rails (~> 4.0.1) ruby depends on
      activesupport (4.0.1)

Can an ActiveHash object belongs_to_active_hash?

Hi!

I need to store all states and cities and I prefer to not use DB for this. Searching for alternatives I've found this GEM. But as far as I understood it doesn't allow this kind of relationship between ActiveHash objects, right?

Thanks.

Use hash for internal data store

I have an ActiveHash model with ~17,000 records. Storing the records in an array and using Enumerable#detect as the primary finder doesn't scale well to this level, so I propose changing the internal data store to a hash map of hashes. This would dramatically speed up .find_by_id queries as id keys are looked up in constant time.

I've pushed a branch, hash_finder, with my suggested changes. The public interface is the same, so data is still loaded in as an array of hashes but converted internally. This patch passes tests in all environments except for REE and Ruby 1.8.7 on Rails 2.2, which has some quirks in its implementation of ActiveSupport::OrderedHash. (The OrderedActiveHash class should eventually be the adapter for the different versions.)

Initial benchmarks show impressive speed gains for .find_by_id queries but some modest losses for .where queries. I know my dataset is unusually large, but perhaps the tradeoff may be worthwhile to others :)

The script I used for benchmarking is https://gist.github.com/3114439. Results for Ruby 1.9 are:

~/code/active_hash 1.9.3@active_hash [master]$ ruby benchmark.rb
user     system      total        real
10th record id  0.010000   0.000000   0.010000 (  0.012161)
10th record where 11.660000   0.010000  11.670000 ( 11.673704)
1000th record id  0.890000   0.000000   0.890000 (  0.881761)
1000th record where 11.750000   0.010000  11.760000 ( 11.763799)

~/code/active_hash 1.9.3@active_hash [hash_finder]$ ruby benchmark.rb 
user     system      total        real
10th record id  0.010000   0.000000   0.010000 (  0.000946)
10th record where 16.540000   0.020000  16.560000 ( 16.572191)
1000th record id  0.000000   0.000000   0.000000 (  0.000934)
1000th record where 16.690000   0.020000  16.710000 ( 16.709648)

And for Ruby 1.8:

~/code/active_hash 1.8.7@active_hash [master]$ ruby benchmark.rb 
 user     system      total        real
10th record id  0.010000   0.000000   0.010000 (  0.013700)
10th record where 19.110000   0.010000  19.120000 ( 19.119064)
1000th record id  1.500000   0.000000   1.500000 (  1.503936)
1000th record where 19.430000   0.010000  19.440000 ( 19.448877)

~/code/active_hash 1.8.7@active_hash [hash_finder]$ ruby benchmark.rb 
user     system      total        real
10th record id  0.000000   0.000000   0.000000 (  0.001701)
10th record where 23.630000   0.020000  23.650000 ( 23.646547)
1000th record id  0.000000   0.000000   0.000000 (  0.001752)
1000th record where 24.030000   0.010000  24.040000 ( 24.043448)

I'm open to comments, suggestions, and other ideas. Especially ideas about making older versions of OrderedHash play nice.

add Associations to ActiveYaml too

whereas it seems to work with ActiveHash, ActiveYaml::Base objects don't seem to be ActiveRecord'able, I get :
uninitialized constant ActiveYaml::Associations

Thanks :)

app that vendors rails 2.3 still attempts to load active_model (rails 3), if present as a local (unvendored) gem

When this condition occurred and was encountered during execution of a rake task, it was difficult to trace back to the cause, since the web app was able to overcome this issue and ran fine.

The log just showed "Anonymous modules have no name to be referenced by" error and other errors that indicated initializers did not execute correctly, since the environment.rb with "config.gem" for active_hash, did not execute correctly, due to the condition in the subject line.

In lib/active_hash.rb you probably want only to require active_model if the version of Rails is 3.0 or greater.

support has_many :through

class Foo < ActiveHash
end

class Bar < AR
  belongs_to :foo
end

class Buzz < AR
  has_many :bars
  has_many :foos, :through => :bars
end

Server errors result in wiping ActiveHashes from memory

Syntax or other errors that reference ActiveHash objects result in wiping all ActiveHash objects from server memory in a irrecoverable way. The only way to restore them is to restart the server, even in development mode. I am initializing about ~15 ActiveHash data structures in a single file in the /initializers directory. Is this expected behavior even in development mode? Shouldn't the data be re-initialized when the development server reloads? How can I minimize the risk of this happening in production mode? This occurs with both Webrick and Thin servers.

Here is an example, assume :gender_id is null:

Gender.find(gender_id).name

This call would result in an error, but also irrecoverably remove all ActiveHash objects from memory and require a server restart, even in development mode.

Create duplicate record entry after update

After we upgraded from version 0.9.7 to 0.9.14 we started receiving the following error every time we try to update a record entry:

Duplicate Id found for record

The following test works at version '0.9.7' but stopped working at commit: ef5ee77

it "updates object in the data collection" do
  Country.all.should be_empty
  country = Country.new :id => 1, :name => "foo"
  country.save
  country.name = "bar"
  country.save!
  Country.all.should == [country]
end

Rails 4 associations

Hi,

I can do ActiveHash -> ActiveRecord associations, but if I try ActiveRecord -> ActiveHash association, I get:

2.0.0p195 :002 > Student.last.gender
  Student Load (1.4ms)  SELECT `students`.* FROM `students` ORDER BY `students`.`id` DESC LIMIT 1
NoMethodError: undefined method `connection' for Gender:Class
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/active_hash-1.0.2/lib/active_hash/base.rb:225:in `method_missing'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/active_hash-1.0.2/lib/active_file/base.rb:51:in `block (2 levels) in singleton class'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association_scope.rb:13:in `initialize'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:100:in `new'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:100:in `association_scope'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:84:in `scope'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/singular_association.rb:42:in `find_target'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:139:in `load_target'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/association.rb:52:in `reload'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/singular_association.rb:9:in `reader'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/builder/association.rb:70:in `gender'
    from (irb):2
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/JozefVaclavik/.rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

# encoding: UTF-8
class Gender < ActiveYaml::Base
    include ActiveHash::Associations
    set_root_path "#{Rails.root}/config/data"

    has_many :students

end
class Student < ActiveRecord::Base
    ... 
    belongs_to :gender
    ...
end

Any ideas?

primary_key unsettable with has_many

foreign_key works just fine, but primary_key always uses id.

Given the following:

# app/models/state.rb
class State < ActiveHash::Base
  include ActiveHash::Associations
  has_many :cities, primary_key: 'abbrev', foreign_key: 'state'

  self.data = [
    { id: 1, name: "California", abbrev: "CA" }
  ]
end

# app/models/city.rb
class City < ActiveRecord::Base
  ... # city.state would be "CA"
end

The following will fail because it looks for the value id and not abbrev

s = State.find_by_name "California"
s.cities

The above would run something like:

SELECT "cities".* FROM "cities"  WHERE "cities"."state" = 1

When it should run:

SELECT "cities".* FROM "cities"  WHERE "cities"."state" = "CA"

I believe it should be fairly easy to add it to lib/associations/associations.rb but I'm pretty green and unsure of all the implications.

document AR 3 dependency

You are using ActiveModel, so bump the dependency to AR3+ otherwise it's kind of confusing when you get uninitialized constant ActiveModel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.