Do Rails 3 Active Record dynamic find_or_create finder methods have some undocumented inconsistencies?

Apologies for the long title, but it bothers me. I am new to Rails, so this is my first project. Rails 3.0.3.

In my model, a Usermay or may not read a lot Entries; This is tracked in a model called ReadEntries. I think this many-to-one relationship is correctly defined in the code.

User.rb:

has_many :read_entries

Entry.rb:

has_many :read_entries

ReadEntry.rb:

belongs_to :entry
belongs_to :user

This table should be filled in at some point. If I try to do this:

user.read_entries.find_or_create_by_entry_id(entry.id, :read => false)

I get an error Unknown key(s): read. Leave trying to install :readand it works.

However, if I create the same line with this, it works:

ReadEntry.find_or_create_by_entry_id_and_user_id(entry.id, user.id, :read => false)

Logically, these methods should be the same, right? Thank.

+3
2

find_or_create. , , .

, , , find_or_create , . :

    permission_assignments.find_or_create_by_role_id(:role_id => role_id, :is_allowed => false)

, , "is_allowed", "true". ( Permission, , )

    PermissionAssignment.find_or_create_by_permission_id_and_role_id(:permission_id => self.id, :role_id => role_id, :is_allowed => false)

, , . , , , ( , - , id), , .

Rails 3.0.4 Postgres 8.4

+1

, Rails, , . :

user.read_entries.find_or_create_by_entry_id_and_read(entry.id, false)

, .

, Rails . , .

+1
source

Source: https://habr.com/ru/post/1785577/


All Articles