New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading integer attribute with wrong value in a relationship #7599
Comments
|
Just to record: My models were 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, :cow_inseminations,
:bull_inseminations, :user_id, :user
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
belongs_to :user
has_one :birth
has_one :sell
has_one :death
has_many :cow_inseminations, :class_name => 'Insemination', :foreign_key => 'cow_id'
has_many :bull_inseminations, :class_name => 'Insemination', :foreign_key => 'bull_id'
# verify if the sex of the animal is 0, so, the animal should have a earring.
def should_have_earring?
self.animal_type.sex == 0 unless self.animal_type.nil?
end
endclass AnimalType < ActiveRecord::Base
attr_accessible :desc, :final_month, :initial_month, :sex
validates :desc, :sex, :final_month, :initial_month, :presence => true
#validates :final_month.months, :date => { :before => :initial_month }
validates_numericality_of :initial_month, :less_than => :final_month
validates_numericality_of :final_month, :greater_than => :initial_month
has_many :animals
endMore info
|
|
Guys, if you need any more info, please let me know... I really have no idea what's going on in this. Thanks |
|
Yeah, I'm not sure. |
|
... oh, and please tell me you didn't create a custom inflection, and your routes are (my sister is a vet, so I smiled at your code...) |
|
Lol, nop,I didn't do that ;) Also, is an app for myfather's form... Btw, have you any ideas of what is going on?i |
|
Wild guess here... your :sex field of type Integer, by any chance is 0 by default? If so, as you are not declaring it in attr_accessible, perhaps is not being stored in the DB... but that still will have to deal with, why in the #index view all is rendered correctly. |
|
The sex field has not any default value. Also, the data is correct in database... I thinking that maybe it's something around these foreign keys.... |
|
Also; This is correct, huh? |
|
Also, rails are using the correct sql: |
|
I write a method in my animal model like this: And it works... looks like when activerecord does the eager load it does not set the sex attribute... |
|
I'm going out on a limb here since you don't describe where you use the attribute, but how are you going about associating AnimalType to Animal. Are you sure that the association is persisted at the point when you check for sex? For example, if you haven't refreshed your Animal instance in a given action then it might not yet reflect the setting of AnimalType. If you've designed your methods (like I'd consider two ways of tracking this down:
|
|
Hi, Yep, I'm sure that's persisted ok. If I go to But, when I do a simple: Then, if I load the animal_type by hand, with a I really think that's something weird here. I also recreate my database, and the problem persists. Also, my MySQL version is 5.5.27-4 from Arch Linux repositories. |
|
Random thought... remove :animal_type (and :user) from attr_accessible but leave :animal_type_id (and :user_id). Technically, you don't set object via mass assignment you set the object_id. |
|
hmm, ok. I'm working right now.. tonight in my home I'll test it and update you about the results. Thanks |
|
I do that, still the same bug... |
|
any other ideas? |
|
I don't.... it would be interesting to see the relevant controller code that precedes But, my only suggestion is reduce method chaining, reduce complexity and increasing logging until the problem becomes apparent. |
|
@palmergs - finding records via an association of @caarlos0 - can you post the results of running |
|
Wild guess here but would you try renaming AnimalType to AnimalKind ? |
|
Sex should probably be an attr of animal unless you have types setup for each sex |
|
what is the status on this issue? Is it sill happening on |
|
I end up changing my model, doesn't know anything about status and/or test case. |
|
Since the ticket owner no longer experiences the problem I'm closing the issue. If someone else has the same bug please comment and I'll reopen and investigate. @caarlos0 thanks for reporting. |
|
That's fine @senny. Thank you and the guys for doing this good job =) |
I think that maybe I found a bug.
I have a Animal and a AnimalType models. A animal belongs to a animal type and a animal type has many animals.
Ok.
Animal type has a attr 'sex', of type integer.
When I load the list of animal types, for example, in the animal_types#index method, everything is ok (the data is correct).
But, when I do a animal.animal_type, the sex always came as 0.
My db is MySQL and I use ruby 1.9.3 and rails 3.2.8.
Any issue related to this? Am I possibly doing something wrong?
BTW, the data in database seems correct to me.
Is it a bug, or is there me doing something wrong? Maybe a database bug?
thanks
The text was updated successfully, but these errors were encountered: